Vb.net在窗体上而非textbox控件上捕捉键盘上按下的键
之前一直苦苦纠结怎么实现这个问题,今天无意间发现了 窗体上有 keypriview这个属性
后经过查找相关资料,了解它是用于窗体捕捉键盘上按下的键喎? f/ware/vc/"="" target="_blank" class="keylink">vcd4ncjxwpta70qrqwr2o0ru49rswzowjrl2rtmvk9nduyejww86qdhj1zaosseo/ydlux+hlycq1z9bv4rj2uabe3kgjpc9wpg0kpha+t723qnk7o7o8yniglz4ncsq508pm4b27zqogbwuua2v5zg93bs3qs8k8l3a+dqo8chjlignsyxnzpq=="brush:java;">
private sub form_keypress(sender as object, e as keyeventargs) handles me.keydown
if e.keycode = keys.escape then
dim messge as new msg
messge.head.text = "即将退出"
messge.msgp.text = "按下enter退出系统,esc返回..."
messge.show()
end if
if e.keycode = keys.f4 then
dim messge as new msg
messge.head.text = "f4"
messge.msgp.text = "你按下了f4"
messge.show()
end if
if e.keycode = keys.f5 then
dim messge as new msg
messge.head.text = "f5"
messge.msgp.text = "你按下了f5"
messge.show()
end if
end sub
这样可以轻松捕捉键盘上的所有键
esc举例:
f4 举例
f5举例
这里再例举一种方法:handles me.keypress
private sub form_keypress(sender as object, e as keypresseventargs) handles me.keypress if e.keychar = chrw(27) then dim messge as new msg messge.head.text = "即将退出系统" messge.msgp.text = "按下enter退出系统,esc返回..." messge.show() end if if e.keychar = chr(115) then dim messge as new msg messge.head.text = "f4" messge.msgp.text = "你按下了f4" messge.show() end if if e.keychar = chr(116) then dim messge as new msg messge.head.text = "f5" messge.msgp.text = "你按下了f5" messge.show() end if end sub
但是,不幸的是这种方式不能捕捉到f1-f12,以及一些特殊的键,比如shift等
当然还可以用handles me.keyup也是可以实现的
喎?>上一篇: 门头沟黄草梁
下一篇: 蒙恬留下两句话,曹操为什么看一次哭一次?