I am running into an odd problem with one particular line involvingGetKeyState. I am attempting to detect a left mouse click of the Windows Start Button outside a form in Visual Basic 2010. The one particular line is very close to being successful but something is wrong. Specific information below.
Public Class Form1
Public Declare Function GetKeyState Lib "user32" (ByVal vKey As Integer) As Short
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If GetKeyState(91) < 0 And GetKeyState(1) < 0 Then
MsgBox("Start Button clicked.")
End If
End Sub
End Class
If I change the line:
If GetKeyState(91) < 0 And GetKeyState(1) < 0 Then
to:
If GetKeyState(91) < 0 Then
I can trigger the MsgBox by clicking the Start Button – outside the form, using only the keyboard. Mouse clicking the Start Button has no response.
If I change the same line to:
If GetKeyState(1) < 0 Then
I can trigger the MsgBox by clicking the left mouse button – outside the form, anywhere in the OS.
However when I try to combine what I perceive would be interpreted as a left mouse click of the Start Button:
If GetKeyState(91) < 0 And GetKeyState(1) < 0 Then
Nothing happens. How do I make it so the MsgBox is triggered when the mouse left-clicks the Start Button?
———————————-Question Addendum—————————————-
I understand now the distinction between the key on the keyboard vs. the button on the screen. Although the Windows Key triggers the Start Button, GetKeyState only accepts input from the keyboard. By name, it would seem that GetKeyState only concerns itself with the state of keys on the keyboard. However, I see a number of GetKeyState functions online that involve both keyboard and mouse.
But I must conclude that GetKeyState is not going to get me to my destination. I ran a program similar to Spy++ called Window Detective and it identifed the Start Button as Parent: Shell_traywnd and the child: Button.
So therefore, it would seem to me that I create something like the following:
myParent = FindWindow(vbNullString, “Shell_traywnd”)
myChild = FindWindowEx(myParent, 0, “Button”, vbNullString)
The question then is – how do I detect if the Child is clicked? In this instance, is the Start Button called a Handle? Or a Window? Also, I know of mouse position using X/Y but I don’t want to use that since a Start Button can be freely relocated in the Windows OS.
Your assumption is incorrect: the GetKeyState parameter of 91 (0x5B) refers to the left windows key on the keyboard, not the start button on the screen. I suspect you’ll need to check where the mouse click occurs.
I’m no expert on this, but you could use Spy++ that comes with Visual Studio to peek at the windows messages involved in clicking the Start button.