Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

How to emulate press and hold of a keystroke?

If you have a question or need help, this is the place to be.
Post Reply
farbox
Experienced User
Posts: 58
Joined: Fri Jul 18, 2008 1:44 am

How to emulate press and hold of a keystroke?

Post by farbox »

Hi all!
Searched the forums but didnt find an answer, so asking here.

How can I make EG on recieving a command from my IR remote, emulate a "press-and-hold" of keys like shift and ctrl?
ie imagine you are selecting a few files in windows file explorer with your mouse, and I want to be able to shift-click, or ctrl-click to select the files I want, as you would do with a keyboard and mouse.
I want to do this since in my setup I always have the mouse and remote handy, and if I can make EG do this, I dont need to dig out the keyboard switch it on etc when I need to selct mulitple files.
I can't get the emulate-keystroke macro to do the job, since having tried it, I think this repeat function just sends repeated keystrokes ( ie shift shift shift shift shift) instead of send and hold (shiffffffffffffffffffffffffffftttttttttttttt) :D .

I can see two ideal methods of making this action work the way I would like it to:
1- EG sends shift keystroke continously as long as the remote is sending the command continously. ie Remote key pressed= shift is on

or

2- Upon pressing the remote button once, EG sends the shift command continuously until the same button is pressed again.Like a shift key-lock, shift key-unlock action. ie remote keypress once= shift is on, remote keypress once more= shift is off

How can I do this please? If there is no standard way to do this, maybe one of the python gurus can knock off a few lines of code easily?

Thanks in advance as always! :)
User avatar
zian
Site Admin
Posts: 569
Joined: Wed Jun 24, 2009 3:54 pm

Re: How to emulate press and hold of a keystroke?

Post by zian »

viewtopic.php?f=2&t=2658
maybe

for next time use google search.... in forum search apparently sucks.
at google use site:http://www.eventghost.net/forum/
eventghost.net
Be there or be square.
farbox
Experienced User
Posts: 58
Joined: Fri Jul 18, 2008 1:44 am

Re: How to emulate press and hold of a keystroke?

Post by farbox »

Zian,
Thank you for that!
I had found that thread before, but it didnt make sense to me while I was quickly scanning thread results...
However with your specific reference to that thread, I re read the thread a few times, and with some tweaking here and there made it work!

So a big thank you from me to you! And a thanks to the author (USER) in the other thread too!
I copy his answer and script below (plus a txt file of the key codes) for future reference in case someone else finds this thread in their search.

PS for some reason Im having difficulty pasting macro configs (the xml code) which I copy from these forums into my EG, ie the PASTE command is greyed out. For example the code at the bottom, the macros wouldnt paste at all before some fiddling in a text file.
When i compare my forum copied text, to the text of a valid macro which i have copied myself from my EG, there are some formatting diffrences, and some lines like
</Macro>
</EventGhost>
<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1640">
etc
are either missing or in the wrong tab position.
So i have to paste them in a text file, compare to a "good" copy, reformat the text, then I can paste them.
And sometimes if I use the "select all" button on the text box, and copy, it doesnt work ( the formatting is wrong, some </Macro> lines are in the wrond tab position), however if I manualy select all the lines and copy, its fine.....
Any ideas why?




Copy of the solution from another thread below:
User wrote:Hello!

I have solved the Problem by reading the code of SendKeys.py. It's all in there - sorry to have bothered you.

For everyone who wants to hold a key down, or toggle a key state via eventghost - it's quite simple with a little python-script:

Code: Select all

sendInputStruct = eg.WinApi.Dynamic.INPUT()
sendInputStruct.type = eg.WinApi.Dynamic.INPUT_KEYBOARD
sendInputStructPointer = eg.WinApi.Dynamic.pointer(sendInputStruct)
sendInputStructSize = eg.WinApi.Dynamic.sizeof(sendInputStruct)
keyboardStruct = sendInputStruct.ki

keyboardStruct.dwFlags = 0
# Uncomment this line to release a key:
# keyboardStruct.dwFlags = eg.WinApi.Dynamic.KEYEVENTF_KEYUP

# Get the Key-Code from SendKeys.py
keyboardStruct.wVk = 91
eg.WinApi.Dynamic.SendInput(1, sendInputStructPointer, sendInputStructSize)
Just copy this code into a new Python-Script-Command. dwFlags = 0 presses the Key down and holds it. You can then use another Macro with the same code and uncomment the line below, so the key will be released by that script. The key will also be released by a normal SendKeys-Command from Event-ghost.

My Config now looks like this:

Code: Select all

<Macro Name="Start Application Switch" Expanded="True">
            <Event Name="X10.ChannelUp" />
            <Action Name="Python Script: Windows-Key down">
                EventGhost.PythonScript(u'sendInputStruct = eg.WinApi.Dynamic.INPUT()\nsendInputStruct.type = eg.WinApi.Dynamic.INPUT_KEYBOARD\nsendInputStructPointer = eg.WinApi.Dynamic.pointer(sendInputStruct)\nsendInputStructSize = eg.WinApi.Dynamic.sizeof(sendInputStruct)\nkeyboardStruct = sendInputStruct.ki\n\nkeyboardStruct.dwFlags = 0\n# Uncomment this line to release a key:\n# keyboardStruct.dwFlags = eg.WinApi.Dynamic.KEYEVENTF_KEYUP\n\n# Get the Key-Code from SendKeys.py\nkeyboardStruct.wVk = 91\neg.WinApi.Dynamic.SendInput(1, sendInputStructPointer, sendInputStructSize)\n')
            </Action>
            <Action>
                Window.SendKeys(u'{Tabulator}', False)
            </Action>
        </Macro>
        <Macro Name="Stop Application Switch" Expanded="True">
            <Event Name="X10.ChannelDown" />
            <Action>
                Window.SendKeys(u'{LWin}', False)
            </Action>
        </Macro>
Quick list of the common key codes to be used with the python script:
KEYEVENTF_KEYUP = 2
VK_SHIFT = 16
VK_CONTROL = 17
VK_MENU = 18

PAUSE = 50/1000.0 # 50 milliseconds

# 'codes' recognized as {CODE( repeat)?}
CODES = {
'BACK': 8,
'BACKSPACE': 8,
'BS': 8,
'BKSP': 8,
'BREAK': 3,
'CAP': 20,
'CAPSLOCK': 20,
'DEL': 46,
'DELETE': 46,
'DOWN': 40,
'END': 35,
'ENTER': 13,
'ESC': 27,
'HELP': 47,
'HOME': 36,
'INS': 45,
'INSERT': 45,
'LEFT': 37,
'LWIN': 91,
'NUMLOCK': 144,
'PGDN': 34,
'PGUP': 33,
'PRTSC': 44,
'RIGHT': 39,
'RMENU': 165,
'RWIN': 92,
'SCROLLLOCK': 145,
'SPACE': 32,
'TAB': 9,
'UP': 38,
'DOWN': 40,
'BACKSPACE': 8,
'F1': 112,
'F2': 113,
'F3': 114,
'F4': 115,
'F5': 116,
'F6': 117,
'F7': 118,
'F8': 119,
'F9': 120,
'F10': 121,
'F11': 122,
'F12': 123,
'F13': 124,
'F14': 125,
'F15': 126,
'F16': 127,
'F17': 128,
'F18': 129,
'F19': 130,
'F20': 131,
'F21': 132,
'F22': 133,
'F23': 134,
'F24': 135,
Attachments
SendKeys.py
(12.71 KiB) Downloaded 244 times
User avatar
zian
Site Admin
Posts: 569
Joined: Wed Jun 24, 2009 3:54 pm

Re: How to emulate press and hold of a keystroke?

Post by zian »

and they said it couldn't/shouldn't be done.
What FOOLS.
lol

Great job farbox.... especially posting your cure.
eventghost.net
Be there or be square.
farbox
Experienced User
Posts: 58
Joined: Fri Jul 18, 2008 1:44 am

Re: How to emulate press and hold of a keystroke?

Post by farbox »

:D
Credit goes to the original poster of the solution, called USER!
I cant code to save my life!
:D

Here is how my macro looks now. I use key 1 for shift-hold, key 3 for Ctrl-hold, and the 2 key (in the middle of the two) for the release action.

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1640">
    <Folder Name="Shift/Ctrl Hold" Expanded="True">
        <Folder Name="Shift key hold" Expanded="True">
            <Macro Name="Hold Shift Down">
                <Event Name="MceRemote.3715" />
                <Action Name="Python Script: shift-Key down">
                    EventGhost.PythonScript(u'sendInputStruct = eg.WinApi.Dynamic.INPUT()\nsendInputStruct.type = eg.WinApi.Dynamic.INPUT_KEYBOARD\nsendInputStructPointer = eg.WinApi.Dynamic.pointer(sendInputStruct)\nsendInputStructSize = eg.WinApi.Dynamic.sizeof(sendInputStruct)\nkeyboardStruct = sendInputStruct.ki\n\nkeyboardStruct.dwFlags = 0\n# Uncomment this line to release a key:\n# keyboardStruct.dwFlags = eg.WinApi.Dynamic.KEYEVENTF_KEYUP\n\n# Get the Key-Code from SendKeys.py\nkeyboardStruct.wVk = 16\neg.WinApi.Dynamic.SendInput(1, sendInputStructPointer, sendInputStructSize)\n')
                </Action>
                <Action>
                    EventGhost.ShowOSD(u'SHIFT', u'0;-24;0;0;0;700;0;0;0;1;0;0;2;32;Arial', (255, 255, 255), None, 3, (0, 0), 0, 3.0, True)
                </Action>
            </Macro>
            <Macro Name="Release">
                <Event Name="MceRemote.3716" />
                <Action>
                    Window.SendKeys(u'{Shift}', False)
                </Action>
            </Macro>
        </Folder>
        <Folder Name="Ctrl key hold" Expanded="True">
            <Macro Name="Hold Ctrl Down">
                <Event Name="MceRemote.3717" />
                <Action Name="Python Script: Ctrl-Key down">
                    EventGhost.PythonScript(u'sendInputStruct = eg.WinApi.Dynamic.INPUT()\nsendInputStruct.type = eg.WinApi.Dynamic.INPUT_KEYBOARD\nsendInputStructPointer = eg.WinApi.Dynamic.pointer(sendInputStruct)\nsendInputStructSize = eg.WinApi.Dynamic.sizeof(sendInputStruct)\nkeyboardStruct = sendInputStruct.ki\n\nkeyboardStruct.dwFlags = 0\n# Uncomment this line to release a key:\n# keyboardStruct.dwFlags = eg.WinApi.Dynamic.KEYEVENTF_KEYUP\n\n# Get the Key-Code from SendKeys.py\nkeyboardStruct.wVk = 17\neg.WinApi.Dynamic.SendInput(1, sendInputStructPointer, sendInputStructSize)\n')
                </Action>
                <Action>
                    EventGhost.ShowOSD(u'Ctrl', u'0;-24;0;0;0;700;0;0;0;1;0;0;2;32;Arial', (255, 255, 255), None, 3, (0, 0), 0, 3.0, True)
                </Action>
            </Macro>
            <Macro Name="Release">
                <Event Name="MceRemote.3716" />
                <Action>
                    Window.SendKeys(u'{Ctrl}', False)
                </Action>
            </Macro>
        </Folder>
    </Folder>
</EventGhost>
For your own use, copy my macro (dont use "select all" it screws the formatting and makes the copied text unrecognisable and unpastabel in EG; manually select all the text and copy), paste it into your EG.
Delete my triggers and add your own desired ones, edit the python script, and use the keycodes from the list to achive the key stroke you want, and you should be all set!
I added an OSD to show that the action has been triggered, it doesnt stay on all the time that the key is held down, so dont expect it to! I still dont know how to do a state indicator OSD!

PS seems the copy paste problem was a bug in EG, depending on the version people were using. See below:

Code: Select all

News in version r1691:

 
    System bugfix - the characters don't get properly encoded in the XML when copying from configuration tree
Post Reply