重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
keypress:操作窗格具有焦点并按下某个键时发生,不能由非字符键引发
十余年的南宁网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整南宁建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“南宁网站设计”,“南宁网站推广”以来,每个客户项目都认真落实执行。
keydown:按下键盘按键时发生
keyup:键盘按键弹起时发生
以上事件触发的时间不同,顺序 down press up
keychar:按键对应的字符
keycode:获取 KeyDown 或 KeyUp 事件的键盘代码
char不能获取 控制键如tab Insert delete 方向键等
怎么会捏,你是不是没有加上mybase.keypress
正确的重写步骤:新建一个Component-类下面一行写上Inherits
TextBox-然后写入:
Protected
Overrides
Sub
OnKeyPress(ByVal
e
As
System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)'这个东东是原来的keypress执行的代码,它会引发keypress事件
'-----这里写代码
End
Sub
定义两个变量
Private run As Boolean = False'过程是否在运行
Private Key_L As Boolean = False'L键是否按下
变通方法:加定时器,要执行的过程放在定时器中调用
keypress事件只检测某个键是否按下
在家里没有VS,引用我以前的例子给你,定义的是全局键盘,也就是在程序不激活的状态也能执行
下面是完整代码: ----按下L键过程xx只会执行一次,直到过程执行完成才再次响应
Public Class Form1
Private run As Boolean = False
Private Key_L As Boolean = False
Public Declare Auto Function RegisterHotKey Lib "user32.dll" Alias _
"RegisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
Public Declare Auto Function UnRegisterHotKey Lib "user32.dll" Alias _
"UnregisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer) As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'注册全局热键
RegisterHotKey(Handle, 0, 7, Keys.C)
RegisterHotKey(Handle, 1, Nothing, Keys.L)
' 0=nothing 1 -alt 2-ctrl 3-ctrl+alt 4-shift 5-alt+shift 6-ctrl+shift 7-ctrl+shift+alt
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
UnRegisterHotKey(Handle, 0)
UnRegisterHotKey(Handle, 1)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = 786 Then
If m.WParam.ToInt32 = 1 Then
Key_L = True
'MsgBox(m.Msg "我1")
ElseIf m.WParam.ToInt32 = 0 Then
MsgBox(m.Msg "我2")
End If
'TextBox1.Text = " " m.Msg
End If
MyBase.WndProc(m)
End Sub
Sub xx()
run = True
Button1.Enabled = False
For i = 0 To 100
TextBox1.Text = i.ToString
Threading.Thread.Sleep(1)
Application.DoEvents()
Next
Button1.Enabled = True
Key_L = False
run = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If run = True Then Exit Sub
If Key_L = True Then xx()
End Sub
End Class