This project has moved. For the latest updates, please go here.

Holding a key

Jul 2, 2014 at 6:18 AM
What I'm looking for is for some way to hold a key while the mover is running, rather than simply send a single keystroke at a time. Say, the 'X' key.

Could it be possible to modify one of the scripts there's an example of here to do this or is this something that would need something more complex?

And could this be an option added to the program?
Coordinator
Jul 2, 2014 at 11:54 AM
Edited Jul 2, 2014 at 12:19 PM
You can use the keybd_event() WinAPI function to achieve this:

http://www.pinvoke.net/default.aspx/user32.keybd_event

Here is some PowerShell code that implements this. It will hold the X key down, wait for five seconds, and then release it.
$KeybdEventSignature = @"
[DllImport("user32.dll")]                                                            
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
"@
$KeybdEvent = Add-Type -memberDefinition $KeybdEventSignature -Name WinApi -passThru
# Press left shift key down
$KeybdEvent::keybd_event([System.Windows.Forms.Keys]::X, 0x45, 0, 0);
Start-Sleep -Seconds 5
# Release left shift key down
$KeybdEvent::keybd_event([System.Windows.Forms.Keys]::X, 0x45, 0x2, 0);
You could break this out into seperate scripts if required, or customise as you see fit.
Jul 21, 2014 at 8:44 AM
Hello, I haven't had a chance to try this out until just today.

I tried to run it but nothing seemed to happen so I turned on the 'show script execution' option.

When the script tried to run it showed this as an error:

Error message

I honestly have very little experience with programming, so I haven't even a clue as to what is or isn't working, beyond that it can't find something.

The script is the exact one you've posted here, no modifications or additions.

Ah, and the reason it shows Yen symbols instead of '/'s is because the computer is set to Japanese system locale which is not something I can change as it's something of a necessity.

Thanks for the help.
Coordinator
Jul 21, 2014 at 9:35 AM
My bad. This code works in PowerShell ISE, but not a PowerShell console window.

Try this revised code, which has an extra line to import the System.Windows.Forms assembly that is required.
$KeybdEventSignature = @"
[DllImport("user32.dll")]                                                            
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
"@
Add-Type -AssemblyName System.Windows.Forms
$KeybdEvent = Add-Type -memberDefinition $KeybdEventSignature -Name WinApi -passThru
# Press left shift key down
$KeybdEvent::keybd_event([System.Windows.Forms.Keys]::X, 0x45, 0, 0);
Start-Sleep -Seconds 5
# Release left shift key down
$KeybdEvent::keybd_event([System.Windows.Forms.Keys]::X, 0x45, 0x2, 0);
Marked as answer by sw3103 on 8/7/2014 at 3:36 AM
Jul 22, 2014 at 4:45 AM
It works perfectly now with the edit.

Thanks for the speed of your reply and for your time.