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

Hello,
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,
A.
OpenOffice 3.2.1, Windows XP/Vista
arrowman
 
Posts: 8
Joined: Mon Nov 21, 2011 12:01 pm

Re: Assign Macro to key event

Postby MrProgrammer » Mon Nov 21, 2011 5:19 pm

Hi, and welcome to the forum.

arrowman wrote:I wanna make macro which move selection to next column when user press "enter" in last row of actual column.
Look in Tools > Customize > Keyboard. Those are the keys that you can assign macros to. If you don't find "Enter" there, you could perhaps achieve the desired result by implementing "cell listeners". You could search for those terms in this forum but I suspect that implementing your macro via cell listeners would be rather complicated. I have not used cell listeners myself and don't know if they get control on Enter or not.

Instead of using a macro, take a look at Tools > Options > OpenOffice.org > General > Press Enter to move selection and see if that is sufficient for your work.
 Edit: I now see that the OP wants to move the selection horizontally only at the last data row of a column so Press Enter to move selection won't help. 


If this answered your question please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.5 Build 9789 on MacOS 10.11.6.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
MrProgrammer
Volunteer
 
Posts: 3583
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

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)
      oCursor.collapseToCurrentRegion()
      if oCursor.RangeAddress.EndRow = oSelection.CellAddress.Row and oCursor.RangeAddress.EndRow <> oCursor.RangeAddress.StartRow then
         KeyHandler_KeyPressed = True
         GoRight
      else
         KeyHandler_KeyPressed = False
      endif
   else
      KeyHandler_KeyPressed = False
   endif
   
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.
Attachments
ooKeyHandle.ods
Handle Enter Key
(14.34 KiB) Downloaded 452 times
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
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
graybeard1ca
 
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 7 guests