在vb.net中获取应用程序/表单加载的关键点

在vb.net中获取应用程序/表单加载的关键点,.net,vb.net,keyboard,application-start,.net,Vb.net,Keyboard,Application Start,我希望能够在应用程序加载时按住一个键,并根据按下的键显示特定的表单 例如,按住shift键并打开itunes会打开一个小对话框,允许您设置库(或其他内容) 我可以检查shift/Ctrl/Alt是否被按下,但我更喜欢使用字母/数字 例如按住1键打开表单1,按住2键打开表单2。如果使用WPF,可以使用键盘。GetKeyStates方法确定单个键的状态。比如说 If KeyBoard.GetKeyStates(Key.D1) = KeyStates.Down Then ' Open Form1

我希望能够在应用程序加载时按住一个键,并根据按下的键显示特定的表单

例如,按住shift键并打开itunes会打开一个小对话框,允许您设置库(或其他内容)

我可以检查shift/Ctrl/Alt是否被按下,但我更喜欢使用字母/数字


例如按住1键打开表单1,按住2键打开表单2。

如果使用WPF,可以使用
键盘。GetKeyStates
方法确定单个
键的状态。比如说

If KeyBoard.GetKeyStates(Key.D1) = KeyStates.Down Then
  ' Open Form1
End If
更多信息:

编辑

对于WinForms来说,解决方案有点难。据我所知,没有一种公开方法可以在
枚举中为您提供公开状态。相反,您必须输入Win32 GetKeyState方法

<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function GetKeyState(ByVal keyCode As Integer) As Short
End Function

如果您想在传统winforms上执行此操作,可以查看本文:

大约在中间有一个抽象键盘类,它使用系统调用来获取键状态。你可能想试试看

编辑:这是转换为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

VB.NET应用程序是否启动了其他进程(如iTunes)?否iTunes就是一个例子,它实现了我想要的功能。它没有启动任何进程。我没有使用WPF,它是windows FormsHanks,你知道一个vb示例吗,我可以理解C#的基础知识,但有些转换更难获得。我从源代码转换了这个逐字记录。希望它能起作用!抱歉花了这么长时间。我还修复了一些打字错误,所以如果它不起作用,请尝试再次复制。
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