The following code works and will print to Label1 when the user presses the a key on the keyboard:
Public Class Form1
Sub New()
InitializeComponent()
End Sub
Public Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal HWND As IntPtr, ByVal WMSG As Integer, ByVal WPARAM As Integer, ByVal LPARAM As IntPtr) As IntPtr
Private WM_KEYDOWN As Integer = &H100
Protected Overrides Sub WndProc(ByRef WindowsMessage As Message)
Select Case WindowsMessage.Msg
Case WM_KEYDOWN
Dim VirtualKeyCode As Integer = CType(WindowsMessage.WParam, Integer)
Select Case (VirtualKeyCode)
Case Keys.A
Label1.Text = "The a key was pressed"
End Select
End Select
MyBase.WndProc(WindowsMessage)
End Sub
End Class
The code stops working after adding (dragging) a button to the form. The WM_KEYDOWN message is no longer sent once the button is on the form. A breakpoint set at Case WM_KEYDOWN never gets hit.
If I remove the button the code starts working again.
Any suggestions?
Thanks.