vb.net 如何捕捉 Ctrl + Alt + RShftKey
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18282303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch Ctrl + Alt + RShftKey
提问by Wine Too
For some time I'm trying to catch Ctrl+ Alt+ Right Shift Keyunder common VBNET key handler. Here are my tests:
有一段时间我试图捕捉Ctrl+ Alt+Right Shift Key下共同VBNET键处理程序。这是我的测试:
If e.Control And e.Alt And e.KeyCode = Keys.Space Then
MsgBox("CTRL + ALT + SPACE") ' This work
End If
If e.Control And e.Shift And e.KeyCode = Keys.F10 Then
MsgBox("CTRL + SHIFT + F10") ' This work
End If
If e.Control And e.Alt And e.KeyCode = Keys.ShiftKey Then
MsgBox("CTRL + ALT + SHIFT") ' This work
End If
If e.Alt And e.Shift And e.KeyCode = Keys.LWin Then
MsgBox("ALT + SHIFT + LEFT WINDOWS") ' This work
End If
If e.Control And e.Alt And e.KeyCode = Keys.RShiftKey Then
MsgBox("CTRL + ALT + RIGHT SHIFT") ' This don't work
End If
Windows 7, WinForms, VB2008, NET framework 2.0
Windows 7、WinForms、VB2008、NET 框架 2.0
Why I can't catch Ctrl+ Alt+ Right Shift Keyin described situation?
Or, how do I catch Ctrl+ Alt+ Right Shift Keycombination?
为什么我不能赶上Ctrl+ Alt+Right Shift Key中所描述的情况呢?
或者说,我怎么赶Ctrl+ Alt+Right Shift Key的组合?
采纳答案by Yuriy Galanter
There is no way to detect difference between Shifts using standard VB.NET approach. You will have to hook into Windows API for that:
无法使用标准 VB.NET 方法检测班次之间的差异。为此,您必须连接到 Windows API:
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function GetAsyncKeyState(vKey As Keys) As Short
End Function
Private Sub Form2_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.Control And e.Alt And e.Shift Then
If Convert.ToBoolean(GetAsyncKeyState(Keys.LShiftKey)) Then
MsgBox("CTRL + ALT + LEFT SHIFT")
ElseIf Convert.ToBoolean(GetAsyncKeyState(Keys.RShiftKey)) Then
MsgBox("CTRL + ALT + RIGHT SHIFT")
End If
End If
End Sub
回答by Hans Passant
Well, this is tricky since these are all modifier keys and the user could press them in any order. You'll need to do some filtering to ensure that a 4th key press doesn't again produce a match, a problem with the accepted answer. And the right-shift key is difficult, it is reported as Keys.Shift when pressed. That requires pinvoke to check if the key is down.
嗯,这很棘手,因为这些都是修饰键,用户可以按任何顺序按下它们。您需要进行一些过滤以确保第 4 次按键不会再次产生匹配,即已接受答案的问题。而且右移键比较难,按下就报Keys.Shift。这需要 pinvoke 来检查密钥是否已关闭。
This worked well:
这工作得很好:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If Control.ModifierKeys = (Keys.Control Or Keys.Alt Or Keys.Shift) Then
If e.KeyCode = Keys.ControlKey Or e.KeyCode = Keys.Menu Or e.KeyCode = Keys.ShiftKey Then
If GetKeyState(Keys.RShiftKey) < 0 And GetKeyState(Keys.LShiftKey) >= 0 Then
MessageBox.Show("yada")
End If
End If
End If
End Sub
Private Declare Function GetKeyState Lib "user32.dll" (ByVal key As Keys) As Short
This works by first verifying that all three modifier keys are down. Then it checks that the last key was pressed was oneof the three keys, the filtering that ensures you don't get too many matches. Finally it checks if the right-shift key is down and it didn't get there by pressing the left-shift as well.
这是通过首先验证所有三个修饰键都已关闭来工作的。然后,它会检查最后一个键被按下了一个三个键,确保你没有得到太多的匹配滤波。最后,它检查右移键是否按下,并且它也没有通过按下左移键到达那里。
回答by rheitzman
Take a look at this:
看看这个:
If e.Control And e.Alt And e.KeyCode = Keys.ShiftKey Then
MsgBox("CTRL + ALT + SHIFT") ' This work
Debug.Print("CTRL + ALT + SHIFT" & GetAsyncKeyState(Keys.ShiftKey) & GetAsyncKeyState(Keys.RShiftKey))
End If
If e.Control And e.Alt And e.KeyCode = Keys.RShiftKey Then
MsgBox("CTRL + ALT + RIGHT SHIFT") ' This don't work
Debug.Print("CTRL + ALT + RIGHT SHIFT " & GetAsyncKeyState(Keys.ShiftKey) & GetAsyncKeyState(Keys.RShiftKey))
End If
You will see that the value for Keys.ShiftKey is the same for left and right. The test for Keys.RShiftKey changes. The DECLARE from above is required for the API call.
您将看到 Keys.ShiftKey 的值对于 left 和 right 是相同的。Keys.RShiftKey 的测试发生了变化。API 调用需要上面的 DECLARE。

