关注 VB.NET 中的 KeyDown?
我见过类似的问题试图解决这个问题,但到目前为止似乎没有一个对我有用...
基本上,对于我的第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您必须将表单属性
KeyPreview
设置为true
。然后您可以使用 from 事件KeyDown
而不是单个按钮的事件First you must set the form property
KeyPreview
totrue
. Then you can use the from eventKeyDown
instead of the event of individual buttons