十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
定义两个变量
创新互联公司从2013年开始,是专业互联网技术服务公司,拥有项目成都网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元易县做网站,已为上家服务,为易县各地企业和个人服务,联系电话:18982081108
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
keypress:操作窗格具有焦点并按下某个键时发生,不能由非字符键引发
keydown:按下键盘按键时发生
keyup:键盘按键弹起时发生
以上事件触发的时间不同,顺序 down press up
keychar:按键对应的字符
keycode:获取 KeyDown 或 KeyUp 事件的键盘代码
char不能获取 控制键如tab Insert delete 方向键等
‘文本框中屏蔽q 或 Q
If Asc(e.KeyChar) = 81 Or Asc(e.KeyChar) = 113 Then
e.KeyChar = ""
End If
'在Button1_KeyDown下代码:
If e.KeyCode = Keys.E Then
Button1.PerformClick()
End If
'只允许输入0至9以及倒退键的示例
If (e.KeyChar = Chr(Asc("0")) And e.KeyChar = Chr(Asc("9"))) Or e.KeyChar = Chr(8) Then Exit Sub
e.KeyChar = Chr(0) ‘拦截其他键值