重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
'只允许输入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) ‘拦截其他键值
'vb全局快捷键是个大大滴难题,不好整。以下是个演示,办法比较笨,本人自用的,你试试。
'窗体放上控件:Command1、Label1、Check1、Check2、Text1、Text2、Timer1
'加入以下代码,运行,设置"确定"键的快捷键
'可选"CTRL+某键"或者"SHIFT+某键"
'"某键"自己设置,只能设置为字母或数字
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer 'API声明
Dim i
'================================================================
'这部分是设置各控件的大小、位置和初始属性,为了便于演示才加的,
'你可以自行设计好各控件的大小、位置和初始属性,无需这些代码。
Private Sub Form_Load()
Form1.Width = 2690: Form1.Height = 2100
Command1.Width = 1215: Command1.Height = 495
Command1.Top = 120: Command1.Left = 120
Command1.Caption = "确定"
Label1.Width = 2175: Label1.Height = 255
Label1.Top = 720: Label1.Left = 120
Label1.Caption = "设置 确定 键的快捷键:"
Check1.Width = 975: Check1.Height = 255
Check1.Top = 960: Check1.Left = 120
Check1.Caption = "CTRL +"
Check2.Width = 975: Check2.Height = 255
Check2.Top = 1320: Check2.Left = 120
Check2.Caption = "SHIFT +"
Text1.Width = 255: Text1.Height = 270
Text1.Top = 960: Text1.Left = 1080
Text1.Text = ""
Text2.Width = 255: Text2.Height = 270
Text2.Top = 1320: Text2.Left = 1080
Text2.Text = ""
Timer1.Interval = 10 'Timer的属性,必须设置
End Sub
'以上部分是设置各控件的大小、位置和初始属性,为了便于演示才加的,
'你可以自行设计好各控件的大小、位置和初始属性,无需这些代码。
'================================================================
Private Sub Timer1_Timer()
If Text1 "" Then
If Len(Text1) 1 Then Text1 = Left(Text1, 1)
If Asc("a") = Asc(Text1) And Asc(Text1) = Asc("z") _
Then Text1 = Chr(Asc(Text1) - 32)
If Check1.Value = 1 And GetAsyncKeyState(vbKeyControl) 0 _
And GetAsyncKeyState(Asc(Text1)) 0 Then
i = i + 1
If i = 1 Then Call Command1_Click
Else
i = 0
End If
End If
If Text2 "" Then
If Len(Text2) 1 Then Text2 = Left(Text2, 1)
If Asc("a") = Asc(Text2) And Asc(Text2) = Asc("z") _
Then Text2 = Chr(Asc(Text2) - 32)
If Check2.Value = 1 And GetAsyncKeyState(vbKeyShift) 0 _
And GetAsyncKeyState(Asc(Text2)) 0 Then
i = i + 1
If i = 1 Then Call Command1_Click
Else
i = 0
End If
End If
End Sub
Private Sub Command1_Click()
print "你好"
SendKeys "你好"
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
Private Sub DataGridView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
Select Case Asc(e.KeyChar)
Case 13
SendKeys.SendWait("{TAB}")
SendKeys.SendWait("{UP}")
End Select
End Sub
keypress:操作窗格具有焦点并按下某个键时发生,不能由非字符键引发
keydown:按下键盘按键时发生
keyup:键盘按键弹起时发生
以上事件触发的时间不同,顺序 down press up
keychar:按键对应的字符
keycode:获取 KeyDown 或 KeyUp 事件的键盘代码
char不能获取 控制键如tab Insert delete 方向键等