控件中keyup属性记录键盘操作
创新互联企业建站,十载网站建设经验,专注于网站建设技术,精于网页设计,有多年建站和网站代运营经验,设计师为客户打造网络企业风格,提供周到的建站售前咨询和贴心的售后服务。对于网站制作、成都网站制作中不同领域进行深入了解和探索,创新互联在网站建设中充分了解客户行业的需求,以灵动的思维在网页中充分展现,通过对客户行业精准市场调研,为客户提供的解决方案。
比如:
Private Sub from1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
if e.keycode = keys.Enter then
lable1.text =Cursor.Position.X
lable2.text = Cursor.Position.Y
end if
end sub
键盘记录没有必要用全局钩子,如果用不好会严重影响系统的效率·VB有一个API函数可以很好的实现键盘记录代码如下:通用部分声明:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer 'API函数的声明dim time as integer添加2个按钮,一个textbox控件。command1_click事件中添加:timer1.enable = truecommand1.enable = falsecommand2.enable = true command2_click :timer2.enable = falsecommand1.enable = truecommand2.enable = false form_load :
command1.enable = truecommand2.enable = false添加一个timer控件,interval属性设置为1000.Enable属性设置为false.并添加下面代码:time = time +1if time = "10" thentimer1.enable = falsecommand1.enable = truecommand2.enable = falseelseFor i = 32 To 256
x = GetAsyncKeyState(i)
If x = -32767 Then
Text1.Text = Text1.Text + Chr(i)
End If
x = GetAsyncKeyState(186)
If x = -32767 Then
Text1.Text = Text1.Text + ";"
End If
x = GetAsyncKeyState(187)
If x = -32767 Then
Text1.Text = Text1.Text + "="
End If
x = GetAsyncKeyState(188)
If x = -32767 Then
Text1.Text = Text1.Text + ","
End If
x = GetAsyncKeyState(189)
If x = -32767 Then
Text1.Text = Text1.Text + "-"
End If
x = GetAsyncKeyState(190)
If x = -32767 Then
Text1.Text = Text1.Text + "."
End If
x = GetAsyncKeyState(191)
If x = -32767 Then
Text1.Text = Text1.Text + "/"
End If'------------------------------
'数字的虚拟键
x = GetAsyncKeyState(96)
If x = -32767 Then
Text1.Text = Text1.Text + "0"
End If
x = GetAsyncKeyState(97)
If x = -32767 Then
Text1.Text = Text1.Text + "1"
End If
x = GetAsyncKeyState(98)
If x = -32767 Then
Text1.Text = Text1.Text + "2"
End If
x = GetAsyncKeyState(99)
If x = -32767 Then
Text1.Text = Text1.Text + "3"
End If
x = GetAsyncKeyState(100)
If x = -32767 Then
Text1.Text = Text1.Text + "4"
End If
x = GetAsyncKeyState(101)
If x = -32767 Then
Text1.Text = Text1.Text + "5"
End If
x = GetAsyncKeyState(102)
If x = -32767 Then
Text1.Text = Text1.Text + "6"
End If
x = GetAsyncKeyState(103)
If x = -32767 Then
Text1.Text = Text1.Text + "7"
End If
x = GetAsyncKeyState(104)
If x = -32767 Then
Text1.Text = Text1.Text + "8"
End If
x = GetAsyncKeyState(105)
If x = -32767 Then
Text1.Text = Text1.Text + "9"
End If
'--------------------------------------
x = GetAsyncKeyState(13)
If x = -32767 Then
Text1.Text = Text1.Text + " (回车键) "
End If
'--------------------------------------
x = GetAsyncKeyState(8)
If x = -32767 Then
Text1.Text = Text1.Text + " (退格键) "
End If
'--------------------------------------
x = GetAsyncKeyState(9)
If x = -32767 Then
Text1.Text = Text1.Text + "(TAB键)"
End If
'--------------------------------------
x = GetAsyncKeyState(16) ''shift键
If x = -32767 And TimeOut = 0 Then
Text1.Text = Text1.Text + "(Shift键)"
End If
'--------------------------------------
x = GetAsyncKeyState(17) ''Ctrl键
If x = -32767 Then
Text1.Text = Text1.Text + "(Ctrl键)"
End If
'--------------------------------------
x = GetAsyncKeyState(18)
If x = -32767 Then
Text1.Text = Text1.Text + "(ALT键)"
End If
'--------------------------------------
x = GetAsyncKeyState(46)
If x = -32767 Then
Text1.Text = Text1.Text + "(删除)"
End If
'--------------------------------------
x = GetAsyncKeyState(38)
If x = -32767 Then
Text1.Text = Text1.Text + "(向上)"
End If
'--------------------------------------
x = GetAsyncKeyState(40)
If x = -32767 Then
Text1.Text = Text1.Text + "(向下)"
End If
'--------------------------------------
x = GetAsyncKeyState(37)
If x = -32767 Then
Text1.Text = Text1.Text + "(向左)"
End If
'--------------------------------------
x = GetAsyncKeyState(39)
If x = -32767 Then
Text1.Text = Text1.Text + "(向右)"
End If
'--------------------------------------x = GetAsyncKeyState(112)
If x = -32767 Then
Text1.Text = Text1.Text + "[F1]"
End Ifx = GetAsyncKeyState(113)
If x = -32767 Then
Text1.Text = Text1.Text + "[F2]"
End Ifx = GetAsyncKeyState(114)
If x = -32767 Then
Text1.Text = Text1.Text + "[F3]"
End Ifx = GetAsyncKeyState(115)
If x = -32767 Then
Text1.Text = Text1.Text + "[F4]"
End Ifx = GetAsyncKeyState(116)
If x = -32767 Then
Text1.Text = Text1.Text + "[F5]"
End Ifx = GetAsyncKeyState(117)
If x = -32767 Then
Text1.Text = Text1.Text + "[F6]"
End Ifx = GetAsyncKeyState(118)
If x = -32767 Then
Text1.Text = Text1.Text + "[F7]"
End Ifx = GetAsyncKeyState(119)
If x = -32767 Then
Text1.Text = Text1.Text + "[F8]"
End Ifx = GetAsyncKeyState(120)
If x = -32767 Then
Text1.Text = Text1.Text + "[F9]"
End Ifx = GetAsyncKeyState(121)
If x = -32767 Then
Text1.Text = Text1.Text + "[F10]"
End Ifx = GetAsyncKeyState(122)
If x = -32767 Then
Text1.Text = Text1.Text + "[F11]"
End Ifx = GetAsyncKeyState(123)
If x = -32767 Then
Text1.Text = Text1.Text + "[F12]"
End IfNext iend if
End Sub
上面的代码中command1为开启按纽,command2为关闭按钮,按下开启按钮后将记录10秒内的键盘消息并在text1中显示我没有在窗体上输出,应为如果消息太多了会显示不好如果你要在窗体输出就改用print函数输出吧
长按你可以用计时器来实现啊,写个例子:
Private blCtrl As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.LControlKey Then blCtrl = True
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.LControlKey Then blCtrl = False
End Sub
然后你可以在计时器里检测blCtrl的状态来确定,不知道你的具体情况是什么样的
总之是用一个布尔变量来记录按键状态,配合计时器来达到你想要的功能