Recording Alt + ? key presses with API hook?

Just starting out? Need help? Post your questions and find answers here.
ignign0kt
User
User
Posts: 59
Joined: Thu Sep 21, 2006 9:09 pm

Recording Alt + ? key presses with API hook?

Post by ignign0kt »

I'm messing around with keyboard hooks, and normal keys catch fine. But I can't figure out how to catch any keys pressed while holding Alt.

Here's what my code looks like:

Code: Select all

Structure PKBDLLHOOKSTRUCT
    vkCode.l
    scanCode.l
    flags.l
    time.l
    dwExtraInfo.l
EndStructure 
 

message.MSG

#VK_ALT = 164

Global shiftDown = #False


OpenFile(0, "c:\test.txt")

Procedure writeToFile(s.s)
    FileSeek(0, Lof(0))         
    WriteString(0,s)
EndProcedure

;############ CallBack function ###############
Procedure.l hookProc(nCode, wParam, lParam)
  *p.PKBDLLHOOKSTRUCT = lParam
       
  If wParam = #WM_KEYDOWN
       
      Select *p\vkCode

        Case #VK_LEFT    :      writeToFile("<Left>")
        Case #VK_RIGHT   :      writeToFile("<Right>")
        Case #VK_UP      :      writeToFile("<Up>")
        Case #VK_DOWN    :      writeToFile("<Down>")
          
        Default
             writeToFile(LCase(Chr(*p\vkCode))) 
                     
      EndSelect
    
   ElseIf wParam = #WM_SYSKEYDOWN
          
         Select *p\vkCode
            Case #VK_ALT     :      writeToFile("<Alt>")
                       
         EndSelect
         
   EndIf
   
        
   ProcedureReturn CallNextHookEx_(#Null, nCode, wParam, lParam)
            
EndProcedure
            
            
            
hInstance = GetModuleHandle_(#Null)

keyboardHook = SetWindowsHookEx_(#WH_KEYBOARD_LL, @hookProc(), hInstance,0)  


;########### Main MSG loop #############
While (GetMessage_(@message,#Null,0,0))
    TranslateMessage_(@message)
    DispatchMessage_(@message)
Wend


UnhookWindowsHookEx_(keyboardHook)    
CloseFile(0)
   
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

ignign0kt
User
User
Posts: 59
Joined: Thu Sep 21, 2006 9:09 pm

Post by ignign0kt »

Sorry I don't see how that helps me in my case with the APIs I'm using
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Once you have the key code you're looking for, say, right arrow, you will then test the context code in the flags member of your KBDLLHOOKSTRUCT structure:

Code: Select all

#LLKHF_ALTDOWN = $20

  [...]
  ElseIf wParam = #WM_SYSKEYDOWN 
          
         Select *p\vkCode 
            Case #VK_RIGHT ; say we're looking for Alt-RightArrow     
              If *p\flags & #LLKHF_ALTDOWN 
                Debug "ALT-RightArrow" 
              Else
                Debug "RightArrow key alone - no alt"
             EndIf 
         EndSelect 
  [...]
BERESHEIT
ignign0kt
User
User
Posts: 59
Joined: Thu Sep 21, 2006 9:09 pm

Post by ignign0kt »

Thanks for the reply, that seems to be working.. couple of questions though :P

I don't understand the use of the the bitwise & for the ALTDOWN flag, when I saw LLKHF_ALTDOWN in the MSDN doc I would have never guessed that's how you use it ;\
Also, does this mean I would have to check *p\flags for every different key press if I wanted to record what was being pressed after ALT?
Post Reply