08-07-2011, 05:56 AM
This time I've decided to share with you a little function from the Win API in user32.dll. It's an invoke method for a virtual keypress.
Here's the general code, in this example i'll show you how to simultate the enter/return keypress event:
You need to do the Imports for System.Runtime.InteropServices so that you can call the API with dllimport. Everything is defined at the top of the class in this example. The button event on the form is what calls the enter keypress event.
VK_ENTER (Virtual Key Enter) I've defined as a Byte with a value of 13 because that's just the defined value for an enter/return keypress. You always need a key_up event otherwise it will only be a keydown event and not a full key press, so that is defined at the top of the button sub as well with a value of 2.
Generates the key down event for the Enter key on your keyboard
Generates the key up event FROM the key {Enter} on your keyboard, you can't have the keyup event without specifying the original key that you are raising the event for. The rest of the values can just be null for now.
There are certain key values for things like the scroll lock, num lock and virtually almost any key on a standard, and some custom keyboard buttons, but this is just the basics, You can get these key events on a timer, or generate them from a registered global hotkey like i've done in the past, it's really up to you on how you use this.
Here's the general code, in this example i'll show you how to simultate the enter/return keypress event:
Code:
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 VirtualKey As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const VK_ENTER As Byte = &H0D
Const KEYEVENT_KEYUP As Byte = &H2
keybd_event(VK_ENTER , 0, 0, 0) 'For KeyDown Event
keybd_event(VK_ENTER , 0, KEYEVENT_KEYUP, 0) 'For KeyUp Event from the Virtual Key {Enter}
End Sub
End Class
You need to do the Imports for System.Runtime.InteropServices so that you can call the API with dllimport. Everything is defined at the top of the class in this example. The button event on the form is what calls the enter keypress event.
VK_ENTER (Virtual Key Enter) I've defined as a Byte with a value of 13 because that's just the defined value for an enter/return keypress. You always need a key_up event otherwise it will only be a keydown event and not a full key press, so that is defined at the top of the button sub as well with a value of 2.
Code:
keybd_event(VK_ENTER , 0, 0, 0)
Code:
keybd_event(VK_ENTER , 0, KEYEVENT_KEYUP, 0)
There are certain key values for things like the scroll lock, num lock and virtually almost any key on a standard, and some custom keyboard buttons, but this is just the basics, You can get these key events on a timer, or generate them from a registered global hotkey like i've done in the past, it's really up to you on how you use this.