Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
keybd_event (user32)
.
It will return a value of false, if there is an error simulating the key press.
<DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, _
CharSet:=CharSet.Unicode, EntryPoint:="keybd_event", _
ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Int32, ByVal dwExtraInfo As Int32) As Boolean
End Function
This sample will generate a 'Scroll Lock' key press event when the user clicks 'Button1'.
Create a new Windows Application project;
Drop a button control in the recently created form;
Add the following code to the form class:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, _
CharSet:=CharSet.Unicode, EntryPoint:="keybd_event", _
ExactSpelling:=True, SetLastError:=True)> _
Public Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer) As Boolean
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const VK_SCROLL As Byte = &H91
Const KEYEVENTF_KEYUP As Byte = &H2
keybd_event(VK_SCROLL, 0x45, 0, 0) ' Generates a KEY_DOWN
keybd_event(VK_SCROLL, 0x45, KEYEVENTF_KEYUP, 0) ' Generates a KEY_UP
End Sub
End Class
Please add some more!
Alternative Managed API: System.Windows.Forms.SendKeys
PROBLEM: How do i use combination of shift and tab keys at the same time ?
public const byte VK_LSHIFT= 0xA0; // left shift key
public const byte VK_TAB = 0x09;
public const int KEYEVENTF_EXTENDEDKEY = 0x01;
public const int KEYEVENTF_KEYUP = 0x02;
//press the shift key
keybd_event(VK_LSHIFT, 0x45, 0, 0);
//press the tab key
keybd_event(VK_TAB, 0x45, 0, 0);
//release the tab key
keybd_event(VK_TAB, 0x45, KEYEVENTF_KEYUP, 0);
//release the shift key
keybd_event(VK_LSHIFT, 0x45, KEYEVENTF_KEYUP, 0);
I'm using this to create automated UI testing for a custom Textbox control. After a lot of trial and error the following code worked well for me.
public partial class Form2 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
void PressKey(Keys key)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
// I had some Compile errors until I Casted the final 0 to UIntPtr like this...
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
}
// Process Results
Compare(sender, expected, NumericTextbox.Text);
}
}
The WaitForInputIdle API
4/8/2011 8:04:06 AM - BBruce-67.99.78.2
The VkKeyScanEx API
2/15/2016 10:34:54 PM - -82.103.64.61
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).