Assign Macro to key event

Creating a macro - Writing a Script - Using the API

Assign Macro to key event

Postby arrowman » Mon Nov 21, 2011 12:14 pm

I would like to assign m\Macro to key "pressed event". Is this possible?

I wanna make macro which move selection to next column when user press "enter" in last row of actual column.

Thank you,
OpenOffice 3.2.1, Windows XP/Vista
Posts: 8
Joined: Mon Nov 21, 2011 12:01 pm

Re: Assign Macro to key event

Postby Charlie Young » Tue Nov 22, 2011 12:00 am

I don't think you can directly assign a macro to the Enter key, but you can intercept it with a KeyHandler and make it run a macro. I had something close to this from an old post somewhere around here, so I'll present it even though it has a few quirks.

The attached spreadsheet has a button in H1 to start and stop the KeyHandler. The main action of the KeyHandler is in the KeyPressed function. Note first of all that setting KeyHandler_KeyPressed = True prevents the default action (such as go down) from happening. The data in B2:F16 are from the old post, and I don't remember what exactly they were for, but they serve the purpose here well enough, I guess.

Code: Select all   Expand viewCollapse view

function KeyHandler_KeyPressed(oEvent) as boolean
   Dim oCursor As Object
   Dim oSelection As Object
   if oEvent.KeyCode = 1280 then
      oSelection = ThisComponent.CurrentController.Selection
      oCursor = ThisComponent.CurrentController.ActiveSheet.createCursorByRange(oSelection)
      if oCursor.RangeAddress.EndRow = oSelection.CellAddress.Row and oCursor.RangeAddress.EndRow <> oCursor.RangeAddress.StartRow then
         KeyHandler_KeyPressed = True
         KeyHandler_KeyPressed = False
      KeyHandler_KeyPressed = False
end function

The major quirks I alluded to are with the method of determining the end of data in the column. Here I am creating a SheetCellCursor and using collapseToCurrentRegion(). This works pretty much, except that the CurrentRegion also includes the surrounding blank columns and/or rows if one of these is selected. There are other methods of dealing with the end of data, but I'll present this here for possible discussion.
Handle Enter Key
(14.34 KiB) Downloaded 614 times
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Assign Macro to key event

Postby graybeard1ca » Sat Feb 17, 2018 1:43 am

Thanks for this! I had a similar need -- to jump from the bottom of one column to the top of the next when entering data. Connecting my recorded and edited macro to Shift+Enter did the trick. Enter a column of data, press SHIFT+ENTER and I'm ready for the next column. :D :D
OpenOffice 4.1.2 on Windows 10
Posts: 1
Joined: Sat Feb 17, 2018 1:40 am

Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 10 guests