How to detect keys pressed if program not current/active?

Just starting out? Need help? Post your questions and find answers here.
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

How to detect keys pressed if program not current/active?

Post by vwidmer »

How can I detect if a key or key combo is pressed if my program is running in the background and not the active program or window?

Thanks
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: How to detect keys pressed if program not current/active

Post by RASHAD »

Windows OS
You can use Keyboard Low Level Hook(Global Hook)
You will detect any key pressed no matter which application is active
You may need Thread for that
Egypt my love
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: How to detect keys pressed if program not current/active

Post by vwidmer »

You know how to do it on linux?

Thanks
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: How to detect keys pressed if program not current/active

Post by RASHAD »

You know how to do it on linux?
No
Next is for windows maybe Shardik or any other guy can do it for Linux

Code: Select all

Global Hook.i, Dim Key.s(256)

Procedure KeyboardHook(nCode, wParam,*k.KBDLLHOOKSTRUCT)
  If nCode < 0 
    ProcedureReturn CallNextHookEx_(hook, nCode, wParam,*k.KBDLLHOOKSTRUCT)
  EndIf
  If wParam=#WM_KEYDOWN
    GetKeyboardState_(@Key())
    ToAscii_(*k\vkCode,*k\scanCode, @Key(), @Ascii.w,0)
    
    If Ascii = 0
      ProcedureReturn CallNextHookEx_(hook, nCode, wParam,*k.KBDLLHOOKSTRUCT)
    ElseIf Ascii = 27
      UnhookWindowsHookEx_(hook)
      End
    Else    
     Debug  Str(Ascii) + " = " + Chr(Ascii)
    EndIf
  EndIf
  ProcedureReturn CallNextHookEx_(hook, nCode, wParam,*k.KBDLLHOOKSTRUCT)
EndProcedure


flags = #PB_Window_Invisible
OpenWindow(0,0,0,100,100,"Test",Flags)

hook=SetWindowsHookEx_(#WH_KEYBOARD_LL, @KeyboardHook(), GetModuleHandle_(0), 0)

Repeat           
  Select WaitWindowEvent(10)
  EndSelect 
ForEver
UnhookWindowsHookEx_(hook)
End
Egypt my love
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: How to detect keys pressed if program not current/active

Post by jack »

please tell me why you want to do this, because I am thinking key logger.
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: How to detect keys pressed if program not current/active

Post by vwidmer »

I am trying to make a notification type window thats hidden then when I press what ever key combo I choose it will pop in from the edge of the screen and show me the info I have in the window when ever I want.

Kinda like how Guake terminal is or I guess like the doom hud or whatever. where they have the ~ key so when u press it terminal shows up.

Thanks
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: How to detect keys pressed if program not current/active

Post by vwidmer »

Something like this I was playing with using the mouse to top right of screen but would like to have key to do it as well.

Excuse the bad coding any one is free to make it better :)

Code: Select all

ExamineDesktops()

OpenWindow(0, DesktopWidth(0)-300, 0, 300, DesktopHeight(0), "Info Window", #PB_Window_BorderLess)
;HideWindow(0,#True)
AddWindowTimer(0, 1, 1000)

Exit = #False
Repeat
  
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
        Debug DesktopMouseX()
        Debug DesktopMouseY()
        If DesktopMouseX() = 0
          SetWindowState(0,#PB_Window_Minimize)
        EndIf
        If DesktopMouseX() = DesktopWidth(0) - 1
          HideWindow(0,#False)
          SetWindowState(0,#PB_Window_Maximize)
          ResizeWindow(0,DesktopWidth(0)-WindowWidth(0),#PB_Ignore,#PB_Ignore,DesktopHeight(0))
        EndIf
        If DesktopMouseX() < (DesktopWidth(0)-WindowWidth(0))
          SetWindowState(0,#PB_Window_Minimize)
        EndIf
      EndIf
    Case #PB_Event_Gadget
    Case #PB_Event_CloseWindow
      Exit = #True
  EndSelect
  
Until Exit
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to detect keys pressed if program not current/active

Post by Dude »

jack wrote:please tell me why you want to do this, because I am thinking key logger.
You need a better imagination. There's LOTS of valid reasons to detect key states.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to detect keys pressed if program not current/active

Post by netmaestro »

For the usage you describe you shouldn't need to do anything fancier than setting a global hotkey for your program. Look up implementation of RegisterHotkey on MSDN.
BERESHEIT
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: How to detect keys pressed if program not current/active

Post by vwidmer »

Thanks but need a linux solution.
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to detect keys pressed if program not current/active

Post by netmaestro »

If I'm not mistaken your Linux equivalent would be XGrabKey. But I know nothing of Linux programming for the most part so I'm really just inviting ridicule on myself.
BERESHEIT
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: How to detect keys pressed if program not current/active

Post by vwidmer »

Yes I seen that https://tronche.com/gui/x/xlib/input/XGrabKey.html

Though I am not a good enough programmer to implement it.
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
Post Reply