Reputation: 68
I'm having trouble with finding a way on how to press specifically a right shift key programmatically. I need a single key down and up (press/release).
What I have is:
SendKeys.Send("{RSHIFT}")
And I know that shift is like:
SendKeys.Send("+")
Which is just shift key, I suppose, but I need specifically a right shift key.
Could, someone, please, help me with this code?
Upvotes: 6
Views: 1137
Reputation: 5215
Using the keybd_event you don't need window handle
VB :
Public Class MyKeyPress
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Sub keybd_event(ByVal bVk As UInteger, ByVal bScan As UInteger, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)
End Sub
' To find other keycodes check bellow link
' http://www.kbdedit.com/manual/low_level_vk_list.html
Public Shared Sub Send(key As Keys)
Select Case key
Case Keys.A
keybd_event(&H41, 0, 0, 0)
Case Keys.Left
keybd_event(&H25, 0, 0, 0)
Case Keys.LShiftKey
keybd_event(&HA0, 0, 0, 0)
Case Keys.RShiftKey
keybd_event(&HA1, 0, 0, 0)
Case Else
Throw New NotImplementedException()
End Select
End Sub
End Class
C#:
public static class MyKeyPress
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void keybd_event(uint bVk, uint bScan, uint dwFlags, uint dwExtraInfo);
// To get other key codes check bellow link
// http://www.kbdedit.com/manual/low_level_vk_list.html
public static void Send(Keys key)
{
switch (key)
{
case Keys.A:
keybd_event(0x41, 0, 0, 0);
break;
case Keys.Left:
keybd_event(0x25, 0, 0, 0);
break;
case Keys.LShiftKey:
keybd_event(0xA0, 0, 0, 0);
break;
case Keys.RShiftKey:
keybd_event(0xA1, 0, 0, 0);
break;
default: throw new NotImplementedException();
}
}
}
usage:
MyKeyPress.Send(Keys.LShiftKey)
Upvotes: 6
Reputation: 318
Found this after some creative keyword combinations
It build down to this for sending keycodes:
Keys key = Keys.RShiftKey;//Right shift key
SendMessage(Process.GetCurrentProcess().MainWindowHandle, WM_KEYDOWN, (int)key, 1);
I don't know what the use case is here, but be mindful of the window handle parameter being passed: Process.GetCurrentProcess().MainWindowHandle
.
This sends the keystroke to itself. If you are trying to send it to another process/program you will need to pass the window handle for that program.
Upvotes: 5