KeyboardPushed() without 2D (Sendkeys?)

Just starting out? Need help? Post your questions and find answers here.
PHP
User
User
Posts: 65
Joined: Sat Sep 10, 2005 5:38 pm

KeyboardPushed() without 2D (Sendkeys?)

Post by PHP »

Hi,

i just wondern why such a function like KeyboardPushed() is only available in 2D and there is no native function...

I need this function but without using 2D-OpenScreen etc.

Found this one somewhere here:

Code: Select all

Global HHOOK

Procedure SendKey(KeySwap.s)
  SwapData.INPUT
  SwapData\type = #INPUT_KEYBOARD
  SwapData\ki\wVk = PeekB(@KeySwap)
  SwapData\ki\wScan = 0
  SwapData\ki\dwFlags = 0
  SwapData\ki\time = 0
  SwapData\ki\dwExtraInfo = 0
  SendInput_(1, SwapData, SizeOf(INPUT))
EndProcedure

Procedure.s GetKeyName(KeyCode.i)
  KeyName.s = Space(#MAX_PATH)
  GetKeyNameText_(MapVirtualKey_(KeyCode, #MAPVK_VK_TO_VSC) * $10000, @KeyName, #MAX_PATH)
  ProcedureReturn KeyName
EndProcedure

Procedure KeyboardHook(nCode, wParam, *kc.KBDLLHOOKSTRUCT)
  If nCode = #HC_ACTION
    KeyCode = *kc\vkCode

    Select KeyCode
      Case 27
        UnhookWindowsHookEx_(HHOOK)
        End
      Case 65 To 90
        Debug GetKeyName(KeyCode)
      Default
        SendKey(#Null$)
        ProcedureReturn 1
    EndSelect
  EndIf
  ProcedureReturn CallNextHookEx_(#Null, nCode, wParam, *kc)
EndProcedure

If OpenWindow(0, 0, 0, 0, 0, "Global Keyboard Hook", #PB_Window_Invisible)
  HHOOK = SetWindowsHookEx_(#WH_KEYBOARD_LL, @KeyboardHook(), #Null, 0)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
That's nice, but produces several outputs in a row just for a short keyboard pushing :-/

I just want the "last" key that has been pushed (no matter if pushed further or released).

Any ideas or other codes?

Thx! :)
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: KeyboardPushed() without 2D (Sendkeys?)

Post by idle »

This is set for the opened window
Process key will print the unicode char and you loop the keys to see which control keys are set

Code: Select all

;-Keyboardhook 
Structure akey
  ks.a[256]
EndStructure  

Global window,myKeyhook
Declare ProcessKeys(key.s,*keys.akey) 

Procedure.i KeyProc(nCode.i,wParam.i,lParam.i)
  
  Protected *keyInput.KBDLLHOOKSTRUCT 
  Static keys.akey 
  Protected ret.i, hwnd.i 
  Protected rs.s=Space(2) 
    
  ret = CallNextHookEx_(myKeyhook, nCode, wParam, lParam) 
  
  *keyInput = lParam 
  
  If nCode = #HC_ACTION 
    
    hwnd = GetForegroundWindow_()
    
    If hwnd = WindowID(window)
      
      Select wParam  
          
        Case #WM_KEYUP
          
          keys\ks[*keyInput\vkCode]= $fe
          
          If (GetAsyncKeyState_(#VK_SHIFT) & $8000)  
            keys\ks[#VK_SHIFT]=$fe
          EndIf 
          
          If (GetAsyncKeyState_(#VK_CONTROL) & $8000)  
            keys\ks[#VK_CONTROL]=$fe
          EndIf 
          
          If (GetAsyncKeyState_(#VK_LMENU) & $8000)  
            keys\ks[#VK_LMENU]=$fe
          EndIf 
          
          If (GetAsyncKeyState_(#VK_RMENU) & $8000)  
            keys\ks[#VK_RMENU]=$fe
          EndIf 
          
                
          If ToUnicode_(*keyInput\vkCode,*keyInput\scanCode,@keys,@rs,1,2) 
             ProcessKeys(rs,@keys)              
          EndIf   
                     
          FillMemory(@keys,256,0) 
          
      EndSelect
    EndIf 
  EndIf    
  
  ProcedureReturn ret
  
EndProcedure 

Procedure SetHooks(hInstance=0)
  If hInstance = 0 
    hInstance = GetModuleHandle_(0)
  EndIf 
  If hInstance 
    myKeyhook = SetWindowsHookEx_(#WH_KEYBOARD_LL, @KeyProc(),hInstance,0) 
  Else 
    MessageRequester("hook", "can't get module handle")
  EndIf       
  
EndProcedure 

Procedure KillHooks()
  UnhookWindowsHookEx_(myKeyhook)
  myKeyHook = 0  
EndProcedure 

Procedure ProcessKeys(key.s,*keys.akey)  
  
  Debug "char " + key 
  For a = 0 To 255 
    If *keys\ks[a] 
      Debug "vkey " + Str(a) 
    EndIf 
  Next
  
EndProcedure   

window = OpenWindow(#PB_Any,0,0,200,100,"test")
If window 
  SetHooks() 
  Repeat 
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
  KillHooks() 
EndIf   
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: KeyboardPushed() without 2D (Sendkeys?)

Post by RASHAD »

No Window required
No Local or Global Hook
Esc to end

Code: Select all

Repeat
  Delay(1)
  For k = 65 To 90
    If GetAsyncKeyState_(#VK_ESCAPE) & $8000 = 32768
      End
    EndIf
    If GetAsyncKeyState_(k) & $8000 = 32768
       keybd_event_(k, 0, 0, 0)
       keybd_event_(k, 0,#KEYEVENTF_KEYUP, 0)  
      If GetAsyncKeyState_(#VK_LSHIFT) & $8000 = 32768 Or GetAsyncKeyState_(#VK_RSHIFT) & $8000 = 32768
         If GetKeyState_(#VK_CAPITAL) = 1               
             Debug Str(Asc(LCase(Chr(k)))) +" = " + LCase(Chr(k))
         Else
             Debug Str(Asc(UCase(Chr(k)))) +" = " + UCase(Chr(k))
         EndIf
      Else
         If GetKeyState_(#VK_CAPITAL) = 1
            Debug Str(Asc(UCase(Chr(k)))) +" = " + UCase(Chr(k))
            ;Debug Chr(200)
            ;Debug Asc("?")          
         Else              
            Debug Str(Asc(LCase(Chr(k)))) +" = " + LCase(Chr(k))
         EndIf
         
      EndIf
    EndIf
  Next
ForEver
Egypt my love
Post Reply