在Keyboard Tester app VB NET中检测Print Screen keyup和keydown

时间:2014-06-20 10:46:43

标签: vb.net

当我尝试为我的小型办公室制作Keyboard Tester应用程序时遇到问题。 我无法检测到打印屏幕键码e.keycode = keys.PrintScreen。

当按下按键时,我会做一些像改变图片框背面颜色的东西,但它似乎不适用于打印屏幕,没有任何反应。

我的代码是:

 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

请帮帮我。如果有更多的按键,如打印屏幕问题,请点击。

谢谢

2 个答案:

答案 0 :(得分:1)

我不确定真正的原因,但我之前已经读过它并得出结论,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

答案 1 :(得分:1)

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

我经常使用这种东西,但是把所有这些IF语句或者Case语句放在一边很疯狂: 只需将框命名为与枚举匹配,然后(假设Controls是所有框的父容器)

Controls(e.keycode.tostring & "box").backcolor = Color.Red

Controls(e.keycode.tostring & "box").backcolor = Color.Transparent

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

当然,您可能想要进行一些检查,如

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