How to simulate a key press via macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
gkick
Posts: 324
Joined: Wed Aug 07, 2019 5:24 pm
Location: Chile

How to simulate a key press via macro

Post by gkick »

Hi all,

How can one assign a key press combination to a macro. For example I would like to have a button which simulates ctrl Q was pressed instead of using the keyboard ?
Thanks for your thoughts
Libre Office 6.4.6 on Windows 10 HSQL 2.51 backend
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to simulate a key press via macro

Post by JeJe »

Do you mean you want to do what ctrl+q does? Using the macro recorder...

Code: Select all

sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
 dispatcher.executeDispatch(document, ".uno:Quit", "", 0, Array())


end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to simulate a key press via macro

Post by JeJe »

If you want to do anything else with ctrl+q you can reassign that shortcut to a macro.

If you really want to simulate that keypress combination then you'll need to use the Windows Api by declaring the appropriate functions at the top of a module

Edit:

http://www.vbforums.com/showthread.php? ... t-Function
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
gkick
Posts: 324
Joined: Wed Aug 07, 2019 5:24 pm
Location: Chile

Re: How to simulate a key press via macro

Post by gkick »

@ Jeje, thanks for the info and link, yes Assigning ctrl q to a macro would be my preferred choice. Do I need to find the keycode or modifier somewhere to do this?
Is there away to this via Tools,Options, Keyboard?

Thanks
Libre Office 6.4.6 on Windows 10 HSQL 2.51 backend
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to simulate a key press via macro

Post by JeJe »

Tools menu / Customise / Keyboard

In the Category listbox there is a choice of LibreOffice macros.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
gkick
Posts: 324
Joined: Wed Aug 07, 2019 5:24 pm
Location: Chile

Re: How to simulate a key press via macro

Post by gkick »

Thank you Jeje, however I may have been unclear. I know how to assign a macro to a key but what I really want is to assign the key to a macro sub. For that I need some reference a la key number or key name pseudo sub appclose blah,execute ctrl q, blah end sub
Libre Office 6.4.6 on Windows 10 HSQL 2.51 backend
User avatar
Zizi64
Volunteer
Posts: 11362
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to simulate a key press via macro

Post by Zizi64 »

I know how to assign a macro to a key but what I really want is to assign the key to a macro sub.
Macro to a key - key to a macro ???????????????????????????
Sorry, I do not understand what is the difference...
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: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to simulate a key press via macro

Post by JeJe »

If you tell us more about what you're trying to achieve with this we might understand you. Otherwise we're back to my earlier answers.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Zizi64
Volunteer
Posts: 11362
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to simulate a key press via macro

Post by Zizi64 »

For example I would like to have a button which simulates ctrl Q
Do you want to launch the default assigned "macros", functions of the hotkeys? There are API equivalent procedures, methods, functions, that you can call instead of "virtual hitting of some hotkeys".

Ctrl-Q = "quit" function. The actual document and the application window will be closed.

Macro equivalent:
https://wiki.openoffice.org/wiki/Closing_a_document
viewtopic.php?f=20&t=30357

Note:
You must examine (by your code) if the document is saved or not.
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.
Bill
Volunteer
Posts: 8934
Joined: Sat Nov 24, 2007 6:48 am

Re: How to simulate a key press via macro

Post by Bill »

gkick wrote:I would like to have a button which simulates ctrl Q was pressed instead of using the keyboard
Where is this button? Is it an icon that you want to add to a Toolbar? Do you want to click the button which then runs a macro that simulates pressing CTRL+Q and closes the document?
AOO 4.1.14 on Ubuntu MATE 22.04
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to simulate a key press via macro

Post by Lupp »

My perception of the question is that the OQer wants to feed keyboard scancodes into the keyboard buffer (if such a thing still exists) and this way send them to the application having the focus.

If so https://duckduckgo.com/?q=send+scancode ... fsb&ia=web might help to find a starting point. Be careful, things will have changed over times!

It's really long ago (DOSage) that I sometimes made a few steps in this field.
However, the techniques will still be used - and needed for many purposes. After all there are barcode scanners, and I suppose their drivers feed scancodes into a keyboard buffer. When I still was a boy of 50, there existed a tool named 'AutoIt' (if I remember correctky) which may have been useful with such a task.
To be clear: I'm not informed about the current state insofar.
Last edited by RoryOF on Wed Apr 08, 2020 4:53 pm, edited 1 time in total.
Reason: Fixed some typos in text [RoryOF, Moderator]
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
gkick
Posts: 324
Joined: Wed Aug 07, 2019 5:24 pm
Location: Chile

Re: How to simulate a key press via macro

Post by gkick »

Thank you all for your valuable pointers and links!
Yes, the basic idea is to have a macro execute a keyboard event such as CTRL Q or other.
For composing such a macro I need either a keycode ala ASCII or a key name or the associated .uno:?
So does anyone know of any doco containing lists of key codes, key mapping
Libre Office 6.4.6 on Windows 10 HSQL 2.51 backend
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to simulate a key press via macro

Post by JeJe »

I'm still not clear what you want to do.

Do you want to make the application quit?

Or do you want to do something else by calling ctrl + q? If so what?

What do you want to make LibreOffice do by this?

Edit:

Window's virtual keycodes are here:

https://docs.microsoft.com/en-us/window ... -key-codes

Just replace 0x at the start with &H to use those codes in Basic - with the Window API I linked to above.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to simulate a key press via macro

Post by Lupp »

As I understood it, theOQer is looking for a (very reduced) version of what is known as a "virtual keyboard" (https://en.wikipedia.org/wiki/Virtual_keyboard). However he doesn't want to install and use extra software for it, but to get the basic functionality by user code inside AOO/LibO: A button's onExecuteAction as the sender simulating the keyboard in a sense, no physical channel/interface, and any object currently having the keyboard focus as the receiver, e.g.
If the button has set TakeFocusOnClick = NO this may work. Different "sensitive areas" could also be used. For me this thread is "over". The related dependencies in modern event-driven software and the OS specifics probably coming into the game are too complicated for me.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply