How to detect keys pressed if program not current/active?
How to detect keys pressed if program not current/active?
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
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
Re: How to detect keys pressed if program not current/active
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
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
Re: How to detect keys pressed if program not current/active
You know how to do it on linux?
Thanks
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
Re: How to detect keys pressed if program not current/active
NoYou know how to do it on linux?
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
Re: How to detect keys pressed if program not current/active
please tell me why you want to do this, because I am thinking key logger.
Re: How to detect keys pressed if program not current/active
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
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
Re: How to detect keys pressed if program not current/active
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
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 ExitWARNING: 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
Re: How to detect keys pressed if program not current/active
You need a better imagination. There's LOTS of valid reasons to detect key states.jack wrote:please tell me why you want to do this, because I am thinking key logger.
- netmaestro
- 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
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
Re: How to detect keys pressed if program not current/active
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
- netmaestro
- 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
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
Re: How to detect keys pressed if program not current/active
Yes I seen that https://tronche.com/gui/x/xlib/input/XGrabKey.html
Though I am not a good enough programmer to implement it.
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

