gpt4 book ai didi

vb6 使用 API 抑制键盘敲击? (制作热键)

转载 作者:行者123 更新时间:2023-12-04 10:14:09 26 4
gpt4 key购买 nike

我已经搜索过,似乎找不到答案,所以任何帮助将不胜感激。

我想制作一个热键,但是当按下热键时,我不希望显示实际的“字符”,而只是要执行的操作。

例如,我有这个:

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

Private Const VK_A = &H41

Private Sub keyboardTimer001_Timer()
If KeyDown(VK_A) Then
' do my stuff, but DONT DISPLAY the letter "A"
End If
end sub

所以这基本上只有一个计时器(间隔 1)检查异步键盘。如果它检测到字母“a”被按下,我执行一个 Action 。但我希望它在不打印字母“a”的情况下做到这一点。
如何从键盘缓冲区中删除键/防止它显示? (旁注 - 不确定“PeekMessage”之类的东西是否可以工作 - 如果可以 - 有谁知道我在哪里可以找到一个好的 vb6 代码示例,我可以在其中查看“ctrl+a”或“ctrl+alt+a”之类的东西,等等,然后只是清除缓冲区,然后执行我的操作?)

谢谢!

最佳答案

您可以使用 RegisterHotKey 和 PeekMessage 的组合。以下代码定义了 Key-A 和 Ctrl-A 来执行操作:

主表格

Option Explicit

Private Done As Boolean

Private Sub Form_Activate()
Done = False
RegisterHotKey Me.hWnd, &HBBBB&, MOD_NONE, vbKeyA
RegisterHotKey Me.hWnd, &HBBBA&, MOD_CONTROL, vbKeyA
ProcessMessages
End Sub

Private Sub ProcessMessages()
Dim Message As Msg

Do While Not Done
WaitMessage

If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
If Message.wParam = &HBBBB& Then
MsgBox "This is my Key-A action"
ElseIf Message.wParam = &HBBBA& Then
MsgBox "This is my Ctrl-A action"
End If
End If

DoEvents
Loop
End Sub

Private Sub Form_Unload(Cancel As Integer)
Done = True
Call UnregisterHotKey(Me.hWnd, &HBBBB&)
Call UnregisterHotKey(Me.hWnd, &HBBBA&)
End Sub

上面的代码运行良好,但在生产应用程序中,我可能倾向于对主窗口进行子类化。如果您更喜欢子类化,则需要使用您选择的技术并替换 ProcessMessages方法是这样的:
Private Function ISubclass_WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case iMsg
Case WM_HOTKEY
If wParam = &HBBBB& Then
MsgBox "This is my Key-A action"
ElseIf wParam = &HBBBA& Then
MsgBox "This is my Ctrl-A action"
End If
End Select
End Function

如您所见,子类化更简洁一些。当然,您需要定义 Win API 的东西。所以在一个模块中,放置以下代码:

模块
Option Explicit

Public Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Public Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Public Declare Function WaitMessage Lib "user32" () As Long

Public Const MOD_NONE = &H0
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const MOD_WIN = &H8
Public Const PM_REMOVE = &H1
Public Const WM_HOTKEY = &H312

Public Type POINTAPI
x As Long
y As Long
End Type

Public Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type

关于vb6 使用 API 抑制键盘敲击? (制作热键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61165407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com