Handling of pressing a key in a Writer form

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Hypno
Posts: 21
Joined: Tue Jun 14, 2022 12:47 pm

Handling of pressing a key in a Writer form

Post by Hypno »

Hi,
I would like to ask for help in handling the KeyPressed event with a Basic macro.
I have a form with the appropriate controls created in Writer. Among other things, there are 2 text fields with the names let's assume Fd1 and Fd2. The data that will be entered into them requires a certain format, which is not possible to obtain with standard text field properties (anyway, some of the available formats work weird ...).
To get this format, when I press the key, I have to intercept this event and, based on the current content of the field, the position of the cursor in the text and, of course, the pressed key, decide what to do next. One possible option is to ignore a key press (e.g. when I don't want to allow a letter to be entered because I am expecting a number).
I wrote earlier about two fields because in both of these fields this format may be different.
It is clear to me that I need to handle the KeyPressed and / or KeyReleased events for this fields. From what I've already found, I should use KeyHandler, but I don't understand how it works.
English is not my native language and that's probably the problem.

Could someone tell me where in the code I should put what declarations and procedures to achieve the above. effect? For example, let's say in the Fd1 field I want to ignore the pressing of the "Q" key and in the Fd2 field the "7" key.
LibreOffice 7.2.2.1 (x64), Windows 10
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Handling of pressing a key in a Writer form

Post by JeJe »

One way would be to delete the Q on the keyrelease event.

Code: Select all

sub keyreleaseevent(ev)
if ev.keychar = "Q" then
st = ev.source.model.text
res = instr(1,st,"Q")
if res<>0 then
mid(st,res,1)=""
ev.source.model.text=st
end if
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Hypno
Posts: 21
Joined: Tue Jun 14, 2022 12:47 pm

Re: Handling of pressing a key in a Writer form

Post by Hypno »

Thanks for the hint,
Of course, it works, and that's the way I use it now. The disadvantage is that the typed letter appears and is deleted, causing the screen to flash.
My point is to ignore pressing a key (under certain conditions) without having to undo the effects of pressing it.
LibreOffice 7.2.2.1 (x64), Windows 10
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Handling of pressing a key in a Writer form

Post by JeJe »

You need to put that information in your initial post so no-one spends their time replicating what you've already looked at.

The events listed for the textbox all look to me to be listeners that don't allow you to modify the text prior to it being changed.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Handling of pressing a key in a Writer form

Post by JeJe »

You have complete control of keyboard input if you use a command button as a stand-in.

Code: Select all

sub keypress(ev)
ev.source.model.label =ev.source.model.label & ev.keychar
end sub
needs code for backspace etc.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply