vb6 suppress keyboard strokes with API? (making a hotkey)

时光怂恿深爱的人放手 提交于 2020-07-21 06:31:59

问题


I've searched, and can't seem to find an answer, so any help would be appreciated.

I want to make a hotkey, but when the hotkey is pressed, I don't want the actual "character" to be displayed, just the action to be performed.

So for example, I have this:

 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

So this basically just has a timer (interval 1) checking the async keyboard. If it detects that the letter "a" was pressed, I perform an action. But I want it to do this WITHOUT printing the letter "a". How would I remove the key from the keyboard buffer/prevent it from displaying? (Side note - not sure if something like 'PeekMessage' would work - if so - does anyone know where I can find a good vb6 code sample where I can peek for stuff like 'ctrl+a' or 'ctrl+alt+a', etc, etc and then just clear the buffer, and perform my action?)

Thanks!


回答1:


You can use a combination of RegisterHotKey and PeekMessage. The following code defines Key-A and Ctrl-A to perform actions:

Main Form

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

The above code works well, but in a production app I might lean towards subclassing the main window. If you prefer subclassing, you will need to use a technique of your choosing and replace the ProcessMessages method with something like this:

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

As you can see, subclassing is a little cleaner. Of course, you need to define the Win API stuff. So in a module, place the following code:

Module

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


来源:https://stackoverflow.com/questions/61165407/vb6-suppress-keyboard-strokes-with-api-making-a-hotkey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!