Vb.net 在键盘测试仪应用程序VB NET中检测打印屏幕上的按键和下键

Vb.net 在键盘测试仪应用程序VB NET中检测打印屏幕上的按键和下键,vb.net,Vb.net,当我试图为我的小办公室制作键盘测试仪应用程序时,我遇到了一个问题。 我检测不到打印屏幕键码e.keycode=keys.PrintScreen 我会在按键按下时更改picturebox的背面颜色,但它似乎不适用于打印屏幕,什么也没有发生 我的代码是: Private Sub keyboardmenu_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 'Esc + Function Keys ---------

当我试图为我的小办公室制作键盘测试仪应用程序时,我遇到了一个问题。 我检测不到打印屏幕键码e.keycode=keys.PrintScreen

我会在按键按下时更改picturebox的背面颜色,但它似乎不适用于打印屏幕,什么也没有发生

我的代码是:

 Private Sub keyboardmenu_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    'Esc + Function Keys -----------------------------------------
    If e.KeyCode = Keys.Escape Then
        EscBox.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F1 Then
        F1Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F2 Then
        F2Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F3 Then
        F3Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F4 Then
        F4Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F5 Then
        F5Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F6 Then
        F6Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F7 Then
        F7Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F8 Then
        F8Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F9 Then
        F9Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F10 Then
        F10Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F11 Then
        F11Box.BackColor = Color.Red
    End If
    If e.KeyCode = Keys.F12 Then
        F12Box.BackColor = Color.Red
    End If
    'End of Esc + Function Keys -----------------------------------------


Private Sub keyboardmenu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp

    'Esc + Function Keys ----------------------------------------

    If e.KeyCode = Keys.F1 Then
        F1Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F2 Then
        F2Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F3 Then
        F3Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F4 Then
        F4Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F5 Then
        F5Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F6 Then
        F6Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F7 Then
        F7Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F8 Then
        F8Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F9 Then
        F9Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F10 Then
        F10Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F11 Then
        F11Box.BackColor = Color.Transparent
    End If
    If e.KeyCode = Keys.F12 Then
        F12Box.BackColor = Color.Transparent
    End If
    'End of Esc + Function Keys -----------------------------------------

End Sub
请帮帮我。Idk如果有更多的按键像打印屏幕的问题


谢谢

我不确定真正的原因,但我以前读过这方面的内容,并得出结论,Windows正在保护该密钥的事件不被轻易处理。其他人可能知道得更清楚,但这是可行的:

Protected Overrides Function ProcessKeyEventArgs(ByRef msg As Message) As Boolean
    If msg.WParam = Keys.PrintScreen Then
        MessageBox.Show("PrintScreen key press detected!")
    End If

    Return MyBase.ProcessKeyEventArgs(msg)
End Function
此外,还应将所有这些if语句放在Select Case语句中:

Private Sub keyboardmenu_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown

    Select Case e.KeyCode
        Case Keys.Escape
            EscBox.BackColor = Color.Red
        Case Keys.F1
            F1Box.BackColor = Color.Red
        Case Keys.F2
            F2Box.BackColor = Color.Red
            'Etc
    End Select

End Sub

此评论是关于IF声明和案例建议的,否则我同意Keith的回答

我经常使用这类东西,但把所有这些IF语句或Case语句放在下面是很疯狂的: 只需命名与枚举匹配的框即可(假设控件是所有框的父容器)

这一行将替换所有内容(如果将escbox重命名为escapebox)

当然,你可能想做一些检查,比如

If Controls.ContainsKey(e.keycode.tostring & "box") Then ...

它和其他键一起工作吗?是的,我还在制作程序的路上,我只是芬兰语的功能键,一切正常。我附上了我的简单代码。没有关于你的问题的建议,但是哎哟,这么多
If
语句
Select Case
是您的朋友(而且速度更快)。更好的方法是使用一个自定义控件,该控件的属性类型为
Keys
。那么你根本不需要这些重复的代码。一个带有属性的自定义控件?什么控件应该有键的属性?它可以工作,但是你能告诉我什么是“ByRef msg as Message”和“CType(CType(msg.WParam,Integer),keys)”?我很高兴它能为你工作。微软会比我解释得更好:另外,如果我的答案是正确的并且对你有效,请将它标记为答案,这样其他人就不会滚动寻找标记的答案。是的,我知道,但等你告诉我,让我检查你的链接,谢谢:)哦,我错过了你的另一个问题:CType是类型转换,但我犯了一个错误。这不需要类型铸造。“如果msg.WParam=Keys.PrintScreen,那么”本身就可以正常工作,因为在本例中,WParam是一个int指针,Keys.PrintScreen还返回一个int。
Controls(e.keycode.tostring & "box").backcolor = Color.Transparent
If Controls.ContainsKey(e.keycode.tostring & "box") Then ...