关注 VB.NET 中的 KeyDown?

发布于 2025-01-01 01:17:26 字数 673 浏览 0 评论 0原文

我见过类似的问题试图解决这个问题,但到目前为止似乎没有一个对我有用...

基本上,对于我的第一个 VB 项目,我正在创建一个虚拟键盘,其中从 KeyDown 上的资源播放声音。到目前为止,该程序似乎可以工作,除了以下事实:在按每个键播放声音之前需要先用鼠标单击每个键(因此将对象置于焦点),我需要按键来播放声音而不需要单击键盘键(将对象置于 KeyDown 上的焦点)。下面是我的代码示例:

Private Sub C_KeyDown(ByVal sender As System.Object, ByVal e As PreviewKeyDownEventArgs) Handles C.PreviewKeyDown ' C is the label attached to the first 'Rectangle' (Key)
    If (e.KeyCode = Keys.A) Then 'The A key should activate the C key on the keyboard 
        My.Computer.Audio.Play(My.Resources.C, _
                                   AudioPlayMode.Background)
    End If
End Sub

我在表单属性下将 KeyPreview 设置为“True”。

谢谢

I have seen similar questions which attempt to solve this issue, but none seem to work for me so far...

Basically, for my first VB project, I am creating a virtual keyboard where a sound is played from the resources on KeyDown. So far, the program seems to work except for the fact that each key needs to be clicked by the mouse before the sound is played by pressing each key (hence put the object in focus), where I need the key to play the sound without clicking the keyboard key (put the object in focus on KeyDown). Below is an example of my code:

Private Sub C_KeyDown(ByVal sender As System.Object, ByVal e As PreviewKeyDownEventArgs) Handles C.PreviewKeyDown ' C is the label attached to the first 'Rectangle' (Key)
    If (e.KeyCode = Keys.A) Then 'The A key should activate the C key on the keyboard 
        My.Computer.Audio.Play(My.Resources.C, _
                                   AudioPlayMode.Background)
    End If
End Sub

I have KeyPreview set to 'True' under the form's properties.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2025-01-08 01:17:26

首先,您必须将表单属性 KeyPreview 设置为 true。然后您可以使用 from 事件 KeyDown 而不是单个按钮的事件

Private Sub MyForm_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    Select Case e.KeyCode
        Case Keys.A
            Console.Beep(440, 200)
        Case Keys.B
            Console.Beep(494, 200)
        Case Keys.C
            Console.Beep(523, 200)
    End Select
End Sub

First you must set the form property KeyPreview to true. Then you can use the from event KeyDown instead of the event of individual buttons

Private Sub MyForm_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    Select Case e.KeyCode
        Case Keys.A
            Console.Beep(440, 200)
        Case Keys.B
            Console.Beep(494, 200)
        Case Keys.C
            Console.Beep(523, 200)
    End Select
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文