Simulate keyboard input to active application

Documentation. First post is always kind of manual page, being continuously updated by author. Following posts are discussion about the topic.
Post Reply
admin
Site Admin
Posts: 441
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

Simulate keyboard input to active application

Post by admin » 12 Jul 2017, 22:02

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
Petr Medek
LUAmacros author

sdasda7777
Posts: 1
Joined: 31 Jul 2017, 14:51

Re: Simulate keyboard input to active application

Post by sdasda7777 » 31 Jul 2017, 14:54

Any way to simulate gamepad (like X360 controller)?
Your program is really cool, and Id be rather using this one program then two separate.
Thanks,
XOXO z Pardubic.

admin
Site Admin
Posts: 441
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

Re: Simulate keyboard input to active application

Post by admin » 02 Aug 2017, 16:19

No, currently not possible and not planned.
Petr Medek
LUAmacros author

otta
Posts: 1
Joined: 17 Oct 2017, 17:22

Re: Simulate keyboard input to active application

Post by otta » 17 Oct 2017, 17:46

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.

admin
Site Admin
Posts: 441
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

Re: Simulate keyboard input to active application

Post by admin » 18 Oct 2017, 08:21

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
Petr Medek
LUAmacros author

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests