Intercepting/modifying keystrokes

Occasional fields in my database contain extended ASCII characters which are not on the keyboard, and which therefore have to be assigned (on a form) to otherwise unused keystroke-combinations. The Key-pressed or Key-released events should be the right context for this. I can find information on how to read what has been pressed, but not on how to replace that by returning a different character.
Guidance, please!

Hello,

A key handler may be helpful (macros involved). It would help to know some examples of characters you have an issue with. What about Unicode? No macro needed for that.

Edit:

Can also right click on text box and select to insert special characters.

The database contains already over 36K entries, all typed by myself. So every economy of keystrokes is worthwhile and saves time. A typical example of what I implemented before is shown by the ObjectPAL (Paradox) code, which is easy enough to understand. This intercepts keystrokes before they are sent to the control.

method keyChar(var eventInfo KeyEvent)
var temp  String Endvar

	temp = eventInfo.char()
	SWITCH
	Case temp = "6":		; Provide caret without using Shift key
		temp = "^"
	Case temp = "#":		; Use hash to generate raised stop
		temp = chrOEM(250)
	Otherwise:
		if eventInfo.IsShiftKeyDown() then	;reverse default action of shift key
			temp = temp.lower()
		Else
			temp = temp.upper()
		Endif
	EndSWITCH
	eventInfo.setChar(temp)
	doDefault
endmethod

Here the caret and raised stop (ASC 183) are regularly used, and text should be always in upper case. This was handled within the keyChar event, which in LibreOffice Base would presumably be the keyPressed (or keyReleased?) event.

@PhilipK,

Have at this time removed my answer as there is a problem with the code. Please note that the code only allows appending - no modifications. Sorry for the inconvenience.

Ah, I haven’t found a problem. The keystrokes generated substitute characters just as intended, and these could be deleted by or committed to the underlying table.

@PhilipK,

Sorry for the inconvenience. You can use the routine as you state. However, if you were to use the arrow keys or mouse to correct something within the text (say at character 5 of 20) the correction would be applied to the end of the text and not where it is needed.

May try a different approach later. This is a problem.

Yes, I did find that - but it didn’t bother me after solving the main problem!

Hello,

Have come up with a method. This uses a key handler. This was done for the ability to trash(dispose of) the key entered.

Here is the code (modified 2020-05-13):

Option Explicit

function KeyHandler_KeyReleased(oEvent) as boolean
    Dim sChar
    Dim sText As String
    Dim sTextMod As String
    Dim iSelMin As Integer
    Dim iSelMax As Integer
    Dim oSelection As New com.sun.star.awt.Selection
    sText = oEvent.Source.GetText
    sTextMod = Ucase(sText)
Rem Left & Right arrow
    If oEvent.KeyCode = 1026 OR oEvent.KeyCode = 1027 Then Exit Function
Rem Insert & Delete
    If oEvent.KeyCode = 1285 OR oEvent.KeyCode = 1286 Then Exit Function
Rem Home & End
    If oEvent.KeyCode = 1028 OR oEvent.KeyCode = 1029 Then Exit Function
Rem Use Replace to substitute characters
    sTextMod = Replace(sTextMod, "6","^")
    sTextMod = Replace(sTextMod, "#",chr(183))
Rem Save position of entry
    iSelMin = oEvent.Source.getSelection().Min
    iSelMax = oEvent.Source.getSelection().Max
Rem Load mdified text back out
    oEvent.Source.setText(sTextMod)
Rem Need to set cursor at last position of data entered
    oSelection.Min = iSelMin
    oSelection.Max = iSelMax
    oEvent.Source.Selection = oSelection
end function

All functions are necessary. The one checking your keys entered is KeyHandler_KeyPressed.

This was tested using a text box for input. Event to be set:

Key released ------ KeyHandler_KeyReleased

The checking of specific key (like insert & delete) and exiting the function were to allow their functionality. This was also done for left & right arrow and Home & End. Depending upon your needs you may need to do more.

This page may be of help → Key.idl File Reference

Edit 2020-05-13:

In light of comments by @PhilipK have placed this code back. Please note this routine only works in certain conditions and may not be suitable for all. Characters may be incorrectly placed in the text.

Edit #2 2020-05-13:

Modified Macro. KeyReleased function is all that is needed. Also replaced IF statements with Replace for character substitution. Can now modify text for a single character at a time (will continue to look for even better solution) instead of placement at end of line all the time.

Edit #3 2020-05-13:

Have now modified code to allow entry anywhere in the text without re-positioning after each character entered.

@PhilipK,

Understood your comment but wasn’t happy with my answer once I realized there was the in line issue. Hope this works better for you and possibly easier with adding further key substitutions if needed/wanted.

:slight_smile:

This seems to be fully perfected now, and should serve a whole range of purposes. Thank you!