Page 1 of 1
How to detect keys pressed if program not current/active?
Posted: Wed Aug 09, 2017 2:45 pm
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
Re: How to detect keys pressed if program not current/active
Posted: Wed Aug 09, 2017 2:56 pm
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
Re: How to detect keys pressed if program not current/active
Posted: Wed Aug 09, 2017 3:18 pm
by vwidmer
You know how to do it on linux?
Thanks
Re: How to detect keys pressed if program not current/active
Posted: Wed Aug 09, 2017 4:08 pm
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
Re: How to detect keys pressed if program not current/active
Posted: Wed Aug 09, 2017 4:33 pm
by jack
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
Posted: Wed Aug 09, 2017 4:46 pm
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
Re: How to detect keys pressed if program not current/active
Posted: Wed Aug 09, 2017 4:49 pm
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
Re: How to detect keys pressed if program not current/active
Posted: Wed Aug 09, 2017 10:45 pm
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.
Re: How to detect keys pressed if program not current/active
Posted: Thu Aug 10, 2017 5:00 pm
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.
Re: How to detect keys pressed if program not current/active
Posted: Thu Aug 10, 2017 5:12 pm
by vwidmer
Thanks but need a linux solution.
Re: How to detect keys pressed if program not current/active
Posted: Thu Aug 10, 2017 5:26 pm
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.
Re: How to detect keys pressed if program not current/active
Posted: Thu Aug 10, 2017 7:55 pm
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.