我希望能够在应用程序正在加载时按住按键,并且根据哪个被按下某个窗体显示。在vb.net的应用程序/表单加载时获得keydown
例如按住shift和打开iTunes打开一个小对话框,允许您设置磁带库(或其它)
我可以检查移位/按Ctrl/Alt键是否被按下,但我更愿意使用字母/数字。
如保持1至打开Form并保持2倒如果使用WPF以打开形式2.
我希望能够在应用程序正在加载时按住按键,并且根据哪个被按下某个窗体显示。在vb.net的应用程序/表单加载时获得keydown
例如按住shift和打开iTunes打开一个小对话框,允许您设置磁带库(或其它)
我可以检查移位/按Ctrl/Alt键是否被按下,但我更愿意使用字母/数字。
如保持1至打开Form并保持2倒如果使用WPF以打开形式2.
如果你想做到这一点对传统的WinForms,你可以看看这篇文章:
http://www.switchonthecode.com/tutorials/winforms-accessing-mouse-and-keyboard-state
关于中途倒有一个使用系统调用的抽象键盘类获取关键状态。您可能想尝试一下。
编辑:这里是该类转换为VB.NET。我没有测试过,所以可能会出现一些错误。让我知道。
Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public MustInherit Class Keyboard
<Flags()>
Private Enum KeyStates
None = 0
Down = 1
Toggled = 2
End Enum
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)>
Private Shared Function GetKeyState(ByVal keyCode As Integer) As Short
End Function
Private Shared Function GetKeyState(ByVal key As Keys) As KeyStates
Dim state = KeyStates.None
Dim retVal = GetKeyState(CType(key, Integer))
' if the high-order bit is 1, the key is down
' otherwise, it is up
If retVal And &H8000 = &H8000 Then
state = state Or KeyStates.Down
End If
' If the low-order bit is 1, the key is toggled.
If retVal And 1 = 1 Then
state = state Or KeyStates.Toggled
End If
Return state
End Function
Public Shared Function IsKeyDown(ByVal key As Keys) As Boolean
Return KeyStates.Down = (GetKeyState(key) And KeyStates.Down)
End Function
Public Shared Function IsKeyToggled(ByVal key As Keys) As Boolean
Return KeyStates.Toggled = (GetKeyState(key) And KeyStates.Toggled)
End Function
End Class
所以一旦你这个类添加到项目中,你可以做这样的事情:
' See if the 1 button is being held down
If Keyboard.IsKeyDown(Keys.D1) Then
' Do the form showing stuff here
EndIf
谢谢,你知道一个VB示例,我可以理解C#的基础知识,但一些转换很难得到。 – 2010-01-15 21:41:07
给我几分钟,我会将它转换为VB给你。 – 2010-01-15 21:42:18
非常感谢:) – 2010-01-15 21:44:56
,可以使用Keyboard.GetKeyStates
方法来确定个体Key
的状态。例如
If KeyBoard.GetKeyStates(Key.D1) = KeyStates.Down Then
' Open Form1
End If
更多信息:
EDIT
为的WinForms溶液是有点困难。我没有暴露的方法,我知道它会给你在Key
枚举中的暴露状态。相反,您必须将PInvoke放入Win32 GetKeyState方法中。
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function GetKeyState(ByVal keyCode As Integer) As Short
End Function
对于大多数键,结果应该可以直接从Key
值转换。
If NativeMethods.GetKeyState(CInt(Key.D1)) < 0 Then
' 1 is held down
End If
我不使用WPF,它是Windows窗体 – 2010-01-15 21:36:39
是VB.NET的应用程序启动的其他进程(即iTunes的)? – Jay 2010-01-15 21:32:37
没有itunes是一个程序的例子,它做我想要我做的事情。它没有启动任何进程。 – 2010-01-15 21:37:55