不用VB当中的键盘(keypress)来捕捉按键,直接用API如何响应呢盘呢??

coolhealth 2003-01-19 04:27:07
我想用API应该会更比它快一点的吧。
...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Clamd 2003-01-20
  • 打赏
  • 举报
回复
'函数声明
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long)as integer

'参数说明
'【操作系统】
'Win9X:Yes
'WinNT:Yes
'【说明】
' 判断函数调用时指定虚拟键的状态
'【返回值】
' Long,自对GetAsyncKeyState函数的上一次调用以来,如键已被按过,则位0设为1;否则设为0。如键目前处于按下状态,则位15设为1;如抬起,则为0。微软的win32手册指出:倘若输入焦点从属于与调用函数的输入线程不同的另一个输入线程,则返回值为0(例如,一旦另一个程序拥有焦点,则它应返回零)。证据显示,函数实际是在整个系统的范围内工作的
'【其它】
' 如指定了VK_LBUTTON 或 VK_RBUTTON,按钮的状态就会根据实际的按钮报告——无论是否曾用SwapMouseButton函数对鼠标的位置进行了交换。win32提供了额外的一些虚拟键码,比如VK_LSHIFT 和 VK_RSHIFT,以便在两个完全一样的键中区分出左右(也包括Ctrl 和 Alt)
'【参数表】
' vKey ----------- Long,欲测试的虚拟键的键码


'示例代码
'Example Name: Trapping Special Key Events using a Low Level Keyboard Hook

'------------------------------------------------------------------------------
'
' BAS Moduel Code
'
'------------------------------------------------------------------------------
Option Explicit

Private Const WH_KEYBOARD_LL = 13& 'enables monitoring of keyboard
'input events about to be posted
'in a thread input queue

Private Const HC_ACTION = 0& 'wParam and lParam parameters
'contain information about a
'keyboard message

Private Const LLKHF_EXTENDED = &H1& 'test the extended-key flag
Private Const LLKHF_INJECTED = &H10& 'test the event-injected flag
Private Const LLKHF_ALTDOWN = &H20& 'test the context code
Private Const LLKHF_UP = &H80& 'test the transition-state flag

Private Const VK_TAB = &H9 'virtual key constants
Private Const VK_CONTROL = &H11
Private Const VK_ESCAPE = &H1B

Private Type KBDLLHOOKSTRUCT
vkCode As Long 'a virtual-key code in the range 1 to 254
scanCode As Long 'hardware scan code for the key
flags As Long 'specifies the extended-key flag,
'event-injected flag, context code,
'and transition-state flag
time As Long 'time stamp for this message
dwExtraInfo As Long 'extra info associated with the message
End Type

Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long

Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal cb As Long)

Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer

Private m_hDllKbdHook As Long 'private variable holding
'the handle to the hook procedure



Public Sub Main()

'set and obtain the handle to the keyboard hook
m_hDllKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, _
AddressOf LowLevelKeyboardProc, _
App.hInstance, _
0&)

If m_hDllKbdHook <> 0 Then

'its hooked!
MsgBox "Ctrl+Esc, Alt+Tab and Alt+Esc are blocked. " & _
"Click OK to quit and re-enable the keys.", _
vbOKOnly Or vbInformation, _
"Keyboard Hook Active"

Call UnhookWindowsHookEx(m_hDllKbdHook)

Else

MsgBox "Failed to install low-level keyboard hook - " & Err.LastDllError

End If

End Sub


Public Function LowLevelKeyboardProc(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long


Static kbdllhs As KBDLLHOOKSTRUCT

If nCode = HC_ACTION Then

Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs))


'Ctrl+Esc --------------
If (kbdllhs.vkCode = VK_ESCAPE) And _
CBool(GetAsyncKeyState(VK_CONTROL) _
And &H8000) Then

Debug.Print "Ctrl+Esc blocked"

LowLevelKeyboardProc = 1
Exit Function

End If 'kbdllhs.vkCode = VK_ESCAPE


'Alt+Tab --------------
If (kbdllhs.vkCode = VK_TAB) And _
CBool(kbdllhs.flags And _
LLKHF_ALTDOWN) Then

Debug.Print "Alt+Tab blocked"

LowLevelKeyboardProc = 1
Exit Function

End If 'kbdllhs.vkCode = VK_TAB


'Alt+Esc --------------
If (kbdllhs.vkCode = VK_ESCAPE) And _
CBool(kbdllhs.flags And _
LLKHF_ALTDOWN) Then

Debug.Print "Alt+Esc blocked"

LowLevelKeyboardProc = 1
Exit Function

End If 'kbdllhs.vkCode = VK_ESCAPE

End If 'nCode = HC_ACTION

LowLevelKeyboardProc = CallNextHookEx(m_hDllKbdHook, _
nCode, _
wParam, _
lParam)

End Function

chanet 2003-01-19
  • 打赏
  • 举报
回复
VB是解译!

I_Iverson 2003-01-19
  • 打赏
  • 举报
回复
其实vb也相当于把api函数封装起来了,而且考虑相当仔细,比你自己用api函数好
zyl910 2003-01-19
  • 打赏
  • 举报
回复
将消息翻译成事件速度基本上没差多少
关键看你的代码的处理速度
tinafang 2003-01-19
  • 打赏
  • 举报
回复
Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧