在键盘测试器应用程序 VB NET 中检测打印屏幕按键和按键 [英] Detect Print Screen keyup and keydown in Keyboard Tester app VB NET

查看:73
本文介绍了在键盘测试器应用程序 VB NET 中检测打印屏幕按键和按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem when I try to make Keyboard Tester application for my small office. I cant detect print screen keycode e.keycode = keys.PrintScreen.

我会做一些事情,比如按下一个键时改变图片框的背景颜色,但它似乎不适用于打印屏幕,什么也没有发生.

I will do something like change picturebox back color when a key down, but it seems doesnt work with print screen, nothing happen.

我的代码是:

 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

请帮帮我.不知道是否还有更多按键,如打印屏幕问题.

Please help me. Idk if there are more keys like print screen problem.

谢谢

推荐答案

我不确定真正的原因,但我以前读过它并得出结论,Windows 正在保护该键的事件不被轻易泄露处理.其他人可能知道得更好,但这有效:

I'm not sure of the real reason, but I've read about it before and came to the conclusion that Windows is protecting that key's event from being easily handled. Someone else probably knows better, but this works:

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 语句中:

Also, you should put all of those if statements in a Select Case statement:

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

这篇关于在键盘测试器应用程序 VB NET 中检测打印屏幕按键和按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆