2011-02-05 141 views
2

我希望能夠檢測何時按下了Windows鍵。我嘗試使用getasynckeystate函數錯誤沒有找到正確的虛擬鍵。 感謝您的幫助!如何檢測Windows按鍵是否被按下?

+1

請正確標記您的問題。你使用C#還是VB?它是VB6還是VB.NET?什麼版本? – Oded 2011-02-05 22:00:22

回答

1

關鍵代碼被賦予Keys Enumeration頁:

倫左Windows徽標鍵(Microsoft自然鍵盤)。
RWin右側Windows徽標鍵(Microsoft Natural Keyboard)。

這並不表明,在使用鍵盤等比Microsoft自然鍵盤時,是否這些代碼是(如果有的話)獲得。

如果你正在使用的WinForms,那麼你需要捕獲KeyDown Event

如果您正在使用WPF,那麼你需要的Keyboard.KeyDown Event

+0

如果它是一個控制檯應用程序,你想調用`Console.ReadKey()`並檢查返回的`ConsoleKeyInfo.Key`屬性的值。值爲`ConsoleKey.LeftWindows`和`ConsoleKey.RightWindows`。 – 2011-02-05 23:09:41

3

檢查了這一點:

Keys Enumeration

你尋找這些關鍵代碼:

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

示例代碼:

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 
1

如果你真的想使用GetAsyncKeyState,你正在尋找WINUSER.H被定義爲VK_LWIN和`VK_RWIN'值:分別0x5B和0x5C。