[Solved] Key Trapping in OOo BASIC

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
zabolyx
Posts: 216
Joined: Fri Aug 07, 2009 7:28 pm

[Solved] Key Trapping in OOo BASIC

Post by zabolyx »

Back in the good ole days of BASIC you had to do key trapping to detect keys as they were pressed.

I've looked through the documentation and I'm not seeing anything using INPUT$ as was with BASIC.

How do I test to see which key has been pressed and or intercept key presses.

I want to remove the end users ability to press the delete key and run a macro that not only deletes the entire record but then compacts the data on the sheet. I just don't know how to trap or intercept the pressed key.
Last edited by zabolyx on Wed Sep 08, 2010 9:46 pm, edited 1 time in total.
OOo 3.1 On Windows XP SP3 (Home)
Running portables of 2.4, 3.0, 3.1, and 3.2 on XP SP3 (Work)
OOo BASIC user

My contribution to the OOo Community code and more
https://sites.google.com/site/ooomacrolog/
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Key Trapping in OOo BASIC

Post by Charlie Young »

I happen to have been playing with that recently. You want an XKeyHandler. Look up that and its related info in the API. The necessary code isn't all that involved.

Note that here, in function KeyHandler_KeyPressed, I just pop up a MsgBox with the keycode (delete = 1286). Pay attention to the Boolean return values though, that is to determine whether or not to "consume" the event, as discussed in the API documentation.

Code: Select all

Option Explicit

global oXKeyHandler as object

sub sStartXKeyHandler
    if isNull(oXKeyHandler) then 'only if not jet running
        oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
        ThisComponent.CurrentController.AddKeyHandler(oXKeyHandler)
    endif
end sub

sub sStopXKeyHandler
    if not IsNull(oXKeyHandler) then 'only if still running
        ThisComponent.CurrentController.removeKeyHandler(oXKeyHandler)
        oXKeyHandler = Nothing 'To know later this handler has stopped.
    end if
end sub

sub XKeyHandler_Disposing(oEvent)
end sub

function KeyHandler_KeyPressed(oEvent) as boolean
    KeyHandler_KeyPressed = False 'cancel KeyPressed
    MsgBox(oEvent.KeyCode)
End function

function KeyHandler_KeyReleased(oEvent) as boolean
    KeyHandler_KeyReleased = False 'cancel KeyReleased
end function
I'm glad someone else is old enough to remember input$ and inkey$. :D
Apache OpenOffice 4.1.1
Windows XP
zabolyx
Posts: 216
Joined: Fri Aug 07, 2009 7:28 pm

Re: Key Trapping in OOo BASIC

Post by zabolyx »

I've successfully adapted this to my needs and can get the key that is pressed.

Issues I am having are...

1. "consume" what you mean Willis? The Release and Press booleans don't seem to keep the program from trying to use the key pressed. This is a locked (protected) sheet and it still comes up with a message that the sheet cannot be edited after the macros that need ran get ran. EDIT: fixed this one... I wasn't setting both of them at the same time.

2.I'm also trying to adapt the double click sort macros you kids whipped up into this to determine the row that is selected when I press a key. The box border is highlighted but not filled in blue like when you drag a selection.

Code: Select all

	'get the current selection
	oCell = oDoc.getCurrentSelection().getCellAddress()
		
	'get the clicked location
	iRowSelect = oCell.Row
	iColSelect = oCell.Column
But I get an error on oCell = oDoc.getCurrentSelection().getCellAddress().

Not sure where to go from here. If I could get those 2 issues to work for me I can get the rest of the deletion macro written.

I'm just looking to completely lock down the program so that the people I work with cannot possibly mangle it and forcing then to delete through a macro keeps things nice and clean (hopefully).
OOo 3.1 On Windows XP SP3 (Home)
Running portables of 2.4, 3.0, 3.1, and 3.2 on XP SP3 (Work)
OOo BASIC user

My contribution to the OOo Community code and more
https://sites.google.com/site/ooomacrolog/
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Key Trapping in OOo BASIC

Post by Charlie Young »

if the current selection is more than one cell it will not have a CellAddress property. Try

Code: Select all

'get the current selection
oCell = oDoc.getCurrentSelection().getCellByPosition(0,0).getCellAddress()
Apache OpenOffice 4.1.1
Windows XP
zabolyx
Posts: 216
Joined: Fri Aug 07, 2009 7:28 pm

Re: Key Trapping in OOo BASIC

Post by zabolyx »

Thank you sir... This should even help with the double click one as well because it will error if more than one cell is selected (which seems to happen on ocassion) as this seems to get just the start of a range.
OOo 3.1 On Windows XP SP3 (Home)
Running portables of 2.4, 3.0, 3.1, and 3.2 on XP SP3 (Work)
OOo BASIC user

My contribution to the OOo Community code and more
https://sites.google.com/site/ooomacrolog/
Post Reply