3

I would like to be able to detect when the windows Key is pressed. I tried with getasynckeystate function bug didnt found the right virtual key. thanks for your help !

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Patrice
  • 31
  • 1
  • 2
  • 1
    Please tag your question correctly. Are you using C# or VB? Is it VB6 or VB.NET? What version? – Oded Feb 05 '11 at 22:00

3 Answers3

3

Check this out:

Keys Enumeration

You're looking for these key codes:

LWin    The left Windows logo key (Microsoft Natural Keyboard).
RWin    The right Windows logo key (Microsoft Natural Keyboard).

Sample code:

Public Sub TextBox1_KeyPress(ByVal sender As Object, _
    ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress

    If (e.Key = Key.LWin Or e.Key = Key.RWin) Then
        MsgBox("Pressed Windows Key")
    End If
End Sub
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
1

The key codes are given on the Keys Enumeration page:

LWin The left Windows logo key (Microsoft Natural Keyboard).
RWin The right Windows logo key (Microsoft Natural Keyboard).

It doesn't indicate whether either (if any) of these codes is obtained when using a keyboard other than the Microsoft Natural Keyboard.

If you are using WinForms then you need to trap the KeyDown Event

If you are using WPF then you need the Keyboard.KeyDown Event

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • And if it's a console application, you want to call `Console.ReadKey()` and check the value of the returned `ConsoleKeyInfo.Key` property. The values are `ConsoleKey.LeftWindows` and `ConsoleKey.RightWindows`. – Jim Mischel Feb 05 '11 at 23:09
1

If you really want to use GetAsyncKeyState, the values you're looking for are defined in WinUser.h as VK_LWIN and `VK_RWIN': 0x5B and 0x5C, respectively.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351