Page 1 of 1

Simulate keyboard input to active application

Posted: 12 Jul 2017, 22:02
by admin
LuaMacros use lmc_send_keys function to simulate key presses to active application. This feature was transferred from HidMacros and uses special format of input string. The format is described at HidMacros help file or at LuaMacros wiki. However this function have limitations (unicode chars), bugs (who said that? :-)) and uses obsolete windows function to simulate keyboard input.

So from LuaMacros version 0.1.1.9 released on July 12th 2017 there's new function lmc_send_input which is very flexible. It is answer to many requirements like unicode support, sending just key press (and release later) and so on. In fact this is just wrapper around windows function SendInput. It takes 3 numeric parameters
  • virtual key code
  • scan code
  • flags
These parameters are described at MSDN page.

Simple scripts that waits 2 seconds and then sends ABc to active application is

Code: Select all

lmc_sleep(2000)
lmc_send_input(16, 0, 0) -- press shift
lmc_send_input(65, 0, 0) -- press A
lmc_send_input(65, 0, 2) -- release A
lmc_send_input(66, 0, 0) -- press B
lmc_send_input(66, 0, 2) -- release B
lmc_send_input(16, 0, 2) -- release shift
lmc_send_input(67, 0, 0) -- press C
lmc_send_input(67, 0, 2) -- release C

Re: Simulate keyboard input to active application

Posted: 31 Jul 2017, 14:54
by sdasda7777
Any way to simulate gamepad?

Re: Simulate keyboard input to active application

Posted: 02 Aug 2017, 16:19
by admin
No, currently not possible and not planned.

Re: Simulate keyboard input to active application

Posted: 17 Oct 2017, 17:46
by otta
Thanks for the update. It helps a lot.

I wanted to add some informations about this:

The SendInput function uses decimal numbers and it is a bit hard to figure out which key is which number, escpacially if this key is not on your keyboard (For example: I wanted hidmacros to send F13-24, which most keyboards don't have, but windows knows these. So I can bind the hotkeys in various tools to those F-Keys... etc it just makes some things a bit simpler).

The link to the MSDN page does not show the actual key-codes, but there is a list of codes for all of them right here MSDN Page

This table shows a list of hexdecimal values which representing certain keys. For example 1 = 0x30. If you are not familiar with hexdicmal values (like me) you can convert them to decimal values with the windows calculator (windows 10). Open it up, set it to "programming" and click on "HEX". Now you can type in the numbers and/or letters after the 0x. HEX 30 is 48 as decimal number. And the decimal number is the one you need for this function.

Re: Simulate keyboard input to active application

Posted: 18 Oct 2017, 08:21
by admin
otta wrote:
17 Oct 2017, 17:46
The SendInput function uses decimal numbers
Not exactly, there's no "dedicated" decimal or hexadecimal number in lua. Just numbers. And you can use hexadecimal annotation in lua:

Code: Select all

> print(0xff)
255