当前位置:编程学习 > VB >>

vb设置热键

比如在全屏游戏中!按F1运行,自动按键A

F2关闭!要说明和代码!

追问:你能帮我做一个游戏的钩子吗?maplestory (冒险岛)

可以后台运行的那种!

答案:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer 

首先声明,这个不多解释

然后添加一个TIMER控件,该控件的主要目的是随时监听程序的按键,然后好执行代码
Private Sub Form_Load()
Timer1.Interval = 100  在窗体启动事件中吧TIMER的时间属性设置为100 自己考虑,数字越小,监听的密度就越大!
End Sub

注意: GetAsyncKeyState 是VB API函数,上面声明的就是这个函数的动态数据库

Private Sub Timer1_Timer() 
If GetAsyncKeyState(vbKeyE) Then '按下E键时窗口显示.
Me.Show
End If
If GetAsyncKeyState(vbKeyY) Then'按下Y键时窗口隐藏.
Me.Hide
End If
End Sub

其他特殊按键

Ctrl,Alt,Shift=vbKeyControl,vbKeyMenu,vbKeyShif

如果还需要其他帮助,请联系我!

你可以用全局的键盘钩子这是我以前找到的一个例子

1 vb做的键盘钩子 
 窗体:
Option Explicit
Dim WithEvents Hook As ClsHook   '创建一个需要事件支持的Hook为模块ClsHook

Private Declare Function MapVirtualKeyEx Lib "user32" Alias "MapVirtualKeyExA" (ByVal uCode As Long, ByVal uMapType As Long, ByVal dwhkl As Long) As Long
 '根据指定的映射类型,执行不同的扫描码和字符转换
 '
 '   uCode Long,欲转换的源字符或代码
 '   uMapType Long,控制映射类型,如下所示
 '           0 —— uCode是个虚拟键码?函数返回相应的扫描码
 '           1 —— uCode是个扫描码?函数返回相应的虚拟键码
 '           2—— uCode是个虚拟键码。函数返回相应的ASCII值(未加Shift组合键)。针对死键,高位设为1。如果出错,返回NULL
 '   dwhkl Long,键盘布局的句柄

 Private Declare Function GetKeyboardLayout Lib "user32" (ByVal dwLayout As Long) As Long
 '取得一个句柄,描述指定应用程序的键盘布局
 '   dwLayout ,//欲检查的线程的标识符
 
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
 '获取与指定窗口关联在一起的一个进程和线程标识符
 '   lpdwProcessId Long,指定一个变量,用于装载拥有那个窗口的一个进程的标识符
 '   hwnd Long,指定窗口句柄

Private Sub Command2_Click()
MsgBox "将窗口最小化之后按下 Ctrl+J 就可以弹出窗体~!"
Me.Hide
End Sub

Private Sub Command1_Click()
 CmdStop.Enabled = False
 Hook.UnHook
 Set Hook = Nothing
End Sub

Private Sub Command3_Click()
Hook.UnHook
Set Hook = Nothing
Unload
End Sub

Private Sub Form_Load()
Set Hook = New ClsHook
Hook.SetHook
End Sub

Private Sub Form_Unload(Cancel As Integer)
 Hook.UnHook
 Set Hook = Nothing
End Sub

Private Sub Hook_KeyDown(KeyCode As Integer, Shift As Integer)  '注意拉,事件~!钩子的KeyDown事件,在模块中我们自己定义的事件KeyDown~!
 Dim StrCode As String
   StrCode = CodeToString(KeyCode)
     '判断Shift
    If StrCode = "[Shift]" Or StrCode = "[Alt]" Or StrCode = "[Ctrl]" Then
      If Shift = vbAltMask + vbCtrlMask Then StrCode = "[Alt + Ctrl]"
      If Shift = vbAltMask + vbShiftMask Then StrCode = "[Alt + Shift]"
      If Shift = vbCtrlMask + vbShiftMask Then StrCode = "[Ctrl + Shift]"
      If Shift = vbCtrlMask + vbShiftMask + vbAltMask Then StrCode = "[Ctrl + Shift +Alt]"
    
    Else
      If Shift = vbShiftMask Then StrCode = StrCode & " + [Shift]"
      If Shift = vbCtrlMask Then StrCode = StrCode & " + [Ctrl]"
      If Shift = vbAltMask Then StrCode = StrCode & " + [Alt]"
      If Shift = vbAltMask + vbCtrlMask Then StrCode = StrCode & " + [Alt + Ctrl]"
      If Shift = vbAltMask + vbShiftMask Then StrCode = StrCode & " + [Alt + Shift]"
      If Shift = vbCtrlMask + vbShiftMask Then StrCode = StrCode & " + [Ctrl + Shift]"
      If Shift = vbCtrlMask + vbShiftMask + vbAltMask Then StrCode = StrCode & " + [Ctrl + Shift +Alt]"
    End If
    
     '热键Ctrl+J,呼出窗口
    If StrCode = "[j] + [Ctrl]" Then
      Me.Show
      App.TaskVisible = True
    End If
    Text1.Text = Text1.Text & "你按下了" & Now & "----" & StrCode & vbCrLf
End Sub

  '把按键码换为String
 
 
 
 作者:wenqianwei    2006-7-19 16:28   回复此发言   
 
--------------------------------------------------------------------------------
 
2 vb做的键盘钩子 
 Private Function CodeToString(nCode As Integer) As String
   Dim StrKey As String
    
     Select Case nCode
          Case vbKeyBack:     StrKey = "BackSpace"
          Case vbKeyTab:      StrKey = "Tab"
          Case vbKeyClear:    StrKey = "Clear"
          Case vbKeyReturn:   StrKey = "Enter"
          Case vbKeyShift:    StrKey = "Shift"
          Case vbKeyControl:  StrKey = "Ctrl"
          Case vbKeyMenu:     StrKey = "Alt"
          Case vbKeyPause:    StrKey = "Pause"
          Case vbKeyCapital:  StrKey = "CapsLock"
          Case vbKeyEscape:   StrKey = "ESC"
          Case vbKeySpace:    StrKey = "SPACEBAR"
          Case vbKeyPageUp:   StrKey = "PAGE UP"
          Case vbKeyPageDown: StrKey = "PAGE DOWN"
          Case vbKeyEnd:      StrKey = "END"
          Case vbKeyHome:     StrKey = "HOME"
          Case vbKeyLeft:     StrKey = "LEFT ARROW"
          Case vbKeyUp:       StrKey = "UP ARROW"
          Case vbKeyRight:    StrKey = "RIGHT ARROW"
          Case vbKeyDown:     StrKey = "DOWN ARROW"
          Case vbKeySelect:   StrKey = "SELECT"
          Case vbKeyPrint:    StrKey = "PRINT SCREEN"
          Case vbKeyExecute:  StrKey = "EXECUTE"
          Case v

上一个:VB是什么
下一个:vb按键语法

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,