[Solved] Trigger a macro with letter key instead of shortcut key

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
joe.dokes
Posts: 16
Joined: Sat Oct 22, 2022 12:37 am

[Solved] Trigger a macro with letter key instead of shortcut key

Post by joe.dokes »

Ok.

1. What I have are external buttons connected to a keyboard emulator, which, when pressed, will type a letter into the selected cell of a spreadsheet.

2. What I want to do is to be able to press one of the buttons and have it run a macro (just like if a macro were assigned to a "shortcut key", which is easy enough to set up).

3. The problem is while the emulator can type letters and numbers, I don't believe it can type a shortcut key (though I could be wrong), and I haven't found anywhere if it's possible to assign a letter or number key to a macro.

4. Here's what I want to know: if a certain letter key (which is not a shortcut key) is ever typed from the emulator, (let's say a "z"), can that be assigned to run a macro? Or alternately if the value of any cell is ever "z", can that be set up to trigger the macro?

Basically, what I want is a button that will, if pressed, delete a certain part of the spreadsheet, all done through the emulator, avoiding any use of a keyboard.

If any of that can be done, how can I go about it?
Last edited by MrProgrammer on Sun Nov 13, 2022 6:32 pm, edited 2 times in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
Open Office 4.1.7, Windows 10
User avatar
Zizi64
Volunteer
Posts: 11365
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by Zizi64 »

Basically, what I want is a button that will, if pressed, delete a certain part of the spreadsheet, all done through the emulator, avoiding any use of a keyboard.
Does your keyboard emulator work by mouse clicks an the graphical letter-buttons? If the answer is Yes:

The toolbar icons work in same way. Assign your macro to user defined toolbar icons/items.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
joe.dokes
Posts: 16
Joined: Sat Oct 22, 2022 12:37 am

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by joe.dokes »

Not quite understanding your question. The emulator has nothing to do with the mouse, mouse clicks have no effect on it.

Letter buttons do, they type letters (if, by "graphical letter-buttons" you mean "regular letters").

So it looks like my answer would have to be "no."
Open Office 4.1.7, Windows 10
joe.dokes
Posts: 16
Joined: Sat Oct 22, 2022 12:37 am

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by joe.dokes »

If what you mean is to click on a graphic of some kind assigned to the macro, I'm already doing that, but I just want to change it so I can be able to just hit a button, not on a keyboard, to do that.
Open Office 4.1.7, Windows 10
User avatar
Hagar Delest
Moderator
Posts: 32670
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by Hagar Delest »

I think that the "emulator" is confusing here and may have been taken as a software layer that displays a keyboard onscreen.
What I understand is that you have an external pad with actual buttons and you use a software layer to apply something to the buttons, be it a letter or a number. Correct?

The problem I see if you use a regular letter, then when you'll hit that letter with your standard keyboard, it will trigger the macro too. Doesn't your emulator has some instruction for such request?
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
joe.dokes
Posts: 16
Joined: Sat Oct 22, 2022 12:37 am

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by joe.dokes »

What I have is pretty much what your first paragraph describes.

Hitting a letter on the keyboard and triggering both the letter and the macro is not a problem at all because I'm not even using a keyboard.

Unfortunately I don't have instructions for the emulator, I bought it long ago and lost it. However they do have customer service so maybe that could be where the answer is :)
Open Office 4.1.7, Windows 10
User avatar
Hagar Delest
Moderator
Posts: 32670
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by Hagar Delest »

If you don't need the keyboard, then it should work. But the Tools > Customize > Keyboard does not allow standard letters.
Maybe there is a compatible key that you could map afterward on your external pad.

Else, you may use a listener, that is a Basic function that "listens" to events like keyboard keys to trigger something like a macro. I'm not very used to that, so search the forum section about macro for "listener".
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
joe.dokes
Posts: 16
Joined: Sat Oct 22, 2022 12:37 am

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by joe.dokes »

Ok thanks for that. I'll keep checking!
Open Office 4.1.7, Windows 10
JeJe
Volunteer
Posts: 2787
Joined: Wed Mar 09, 2016 2:40 pm

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by JeJe »

If you run sStartXKeyHandler (only do it once) then whenever you type an A in a cell in the active document controller when you ran that macro it will show a message box instead of the A- you can replace that line with your macro.

Code: Select all

sub sStartXKeyHandler

	dim oDoc,titlest as string
	oDoc =thiscomponent

	oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
	oDoc.CurrentController.AddKeyHandler(oXKeyHandler)
end sub

sub sStopXKeyHandler
end sub

sub KeyHandler_Disposing(oEvent)
end sub

function KeyHandler_KeyPressed(ev) as boolean
if ev.keychar = "A" then
'run macro
msgbox "A"
 KeyHandler_KeyPressed=true
else
 KeyHandler_KeyPressed=false
end if
End function

function KeyHandler_KeyReleased(oEvent) as boolean
end function


Edit: slight mod.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
joe.dokes
Posts: 16
Joined: Sat Oct 22, 2022 12:37 am

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by joe.dokes »

JeJe, about what you said above:

I did it and in the line that says msgbox "A" I replaced the "A" with the name of my macro. What I got was the message box with the name of my macro in it, which, I suppose, is progress. At least typing the "A" made something happen.

But what I need is for the macro to actually run instead of putting up a message box with the name of the macro in it. To make it work like that what does the line msgbox "A" (or msgbox "nameofmymacro") need to say? Or is there something other than that that needs to be changed too?
Open Office 4.1.7, Windows 10
FJCC
Moderator
Posts: 9284
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by FJCC »

Replace the entire line
msgbox "A"
with the name of your macro
nameofmymacro
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Zizi64
Volunteer
Posts: 11365
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by Zizi64 »

If your "emulator" is not a software, but it is a (configurable) external physical device; then why you not use a cheap, physical PC keyboard what has SHIFT, CTRL, ALT, ALTGR keys? Both wired and wireless versions are available in stores...


If you have some information about the type of your device, then you can search the manual/description on the page of the manufacturer or on the archived manuals pages. Maybe the controlling keys are configurable on your device.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2787
Joined: Wed Mar 09, 2016 2:40 pm

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by JeJe »

Msgbox is an inbuilt OO Basic function to show a message box with whatever string you put in the quotes after it. Its a convenient substitute function to show that whatever you put at that point in the code is going to be called.

As FJCC says you replace the whole line with your own function call or your own code.

Edit:

Code: Select all

sub sStartXKeyHandler

	dim oDoc,titlest as string
	oDoc =thiscomponent

	oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
	oDoc.CurrentController.AddKeyHandler(oXKeyHandler)
end sub

sub sStopXKeyHandler
end sub

sub KeyHandler_Disposing(oEvent)
end sub

function KeyHandler_KeyPressed(ev) as boolean
if ev.keychar = "A" then

YourSubOrFunctionName 'call your macro below
'or just put the code here instead
 KeyHandler_KeyPressed=true
else
 KeyHandler_KeyPressed=false
end if
End function

function KeyHandler_KeyReleased(oEvent) as boolean
end function

Sub YourSubOrFunctionName()
' whatever your code is
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
joe.dokes
Posts: 16
Joined: Sat Oct 22, 2022 12:37 am

Re: Can I trigger a macro by pressing a letter key instead of a shortcut key?

Post by joe.dokes »

SOLVED! Just by doing what FJCC said to do.

Thanks everyone who contributed to this!
Zizi asked "If your "emulator" is not a software, but it is a (configurable) external physical device; then why you not use a cheap, physical PC keyboard what has SHIFT, CTRL, ALT, ALTGR keys? Both wired and wireless versions are available in stores..."
While having a keyboard would be the easy way, I was going for a so-called "elegent solution" with just one button instead of having a big old bulky keyboard just to type one fairly uncommon key. Plus its a cool looking button too :D

Thanks for helping me achieve this!
Open Office 4.1.7, Windows 10
User avatar
Zizi64
Volunteer
Posts: 11365
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] Trigger a macro with letter key instead of shortcut key

Post by Zizi64 »

While having a keyboard would be the easy way, I was going for a so-called "elegent solution" with just one button instead of having a big old bulky keyboard just to type one fairly uncommon key. Plus its a cool looking button too
:crazy: :crazy: :crazy:
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Hagar Delest
Moderator
Posts: 32670
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Trigger a macro with letter key instead of shortcut key

Post by Hagar Delest »

Zizi64 wrote: Sun Nov 13, 2022 8:13 pm :crazy: :crazy: :crazy:
???
joe.dokes' expectation makes pretty good sense.
Mere emoticons may be subject to various interpretations.
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11365
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] Trigger a macro with letter key instead of shortcut key

Post by Zizi64 »

When somebody choose an exotic and "cool looking" tool then a cool looking Manual and driver/configurator software is required for it.
Mere emoticons may be subject to various interpretations.
:crazy: this is my head in this case = "Why I tried to help? It was a wasted time"
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Hagar Delest
Moderator
Posts: 32670
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Trigger a macro with letter key instead of shortcut key

Post by Hagar Delest »

We can't be always right.
:lol:
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
joe.dokes
Posts: 16
Joined: Sat Oct 22, 2022 12:37 am

Re: [Solved] Trigger a macro with letter key instead of shortcut key

Post by joe.dokes »

If you could see my project, you'd know exactly why I was trying to accomplish what I started this thread for. The project calls for me to do it without a keyboard. You really did get me out of a problem, and showed myself, and maybe other people, how to do something.

Thanks again for the help!
Open Office 4.1.7, Windows 10
User avatar
RoryOF
Moderator
Posts: 34621
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] Trigger a macro with letter key instead of shortcut key

Post by RoryOF »

In the early days of microcomputers, when they were seen as "controllers", input to trigger a program was often achieved by monitoring a switch attached to an input/output port, often a printer port, but sometimes a line to a serial port. These usually needed some code, which was often written in BASIC.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Post Reply