Page 1 of 1

Anyway to query hot keys?

Posted: Tue Dec 16, 2014 2:13 am
by jassing
I am writing a portion of a larger app... Timezone & language is getting in the way...

On entry into a field, if possible, I need to add some hotkeys; but if those hotkeys are already taken, I need to "push them" down and restore them when field losses focus...

is there anyway to query PB if a hotkey is present and what it is? or use windows api? It's a windows app only so I do not need cross platform...

Re: Anyway to query hot keys?

Posted: Tue Dec 16, 2014 2:38 am
by missile69
On Windows, if you can try to register a hotkey using the api call RegisterHotKey. If it fails (returns 0), it means another program is already using the requested key combination. You can see my post regarding global windows hotkeys for an example of using that api call here:

http://www.purebasic.fr/english/viewtop ... 12&t=61117

If by "push them down", you mean you want to use a hotkey even though another program is already using it, maybe you can set up a keyboard hook to intercept the key combination. If you don't want the other program to receive the key presses, just don't pass on the message. See my keyboard hook example here:

http://www.purebasic.fr/english/viewtop ... 14&t=61154

Re: Anyway to query hot keys?

Posted: Tue Dec 16, 2014 4:36 am
by jassing
Let's say the application has "CTRL-D" assigned to #menuDeleteItem
When entering a specific field, I want ctrl-d to be assigned to #menuDebugOn, on exit, I want to return to #menuDeleteItem.

so I want to
On Focus:
oldmenu = QueryKeyboardShortcut(#MyWindow,#pb_shortcut_control|#pb_shortcut_d)
AddKeyboardShortcut(#MyWindow,#pb_shortcut_control|#pb_shortcut_d, #menuDebugOn)

On LostFocus
AddKeyboardShortcut(#mywindow,#pb...., oldmenu)

Re: Anyway to query hot keys?

Posted: Tue Dec 16, 2014 5:07 am
by missile69
Oh, okay. I misunderstood. If you did already know that Ctrl+D is for #menuDeleteItem, you can just overwrite a keyboard shortcut when your text field gets focus, and then overwrite that again with another call to AddKeyboardShortcut on LostFocus without having to first call RemoveKeyboardShortcut, but I don't know how to tell if a keyboard shortcut added with AddKeyboardShortcut already exists.

Re: Anyway to query hot keys?

Posted: Tue Dec 16, 2014 5:28 am
by RASHAD
Hi jassing
Search the forum for hot keys by DarkPlayer
If I can get some free time maybe I can help

Re: Anyway to query hot keys?

Posted: Tue Dec 16, 2014 5:41 pm
by jassing
Thanks Rashad, but sadly that search pulled up no results.
but knowing there's an answer means I'll keep digging. thank you!
-josh

Re: Anyway to query hot keys?

Posted: Tue Dec 16, 2014 5:56 pm
by IdeasVacuum

Re: Anyway to query hot keys?

Posted: Tue Dec 16, 2014 6:56 pm
by jassing
I will print & read that thread out.. On 1st pass; I don't see how to query if a hotkey already exists and what it is... I'm missing something...
thanks

Re: Anyway to query hot keys?

Posted: Wed Dec 17, 2014 2:01 am
by RASHAD
Can you submit a little example to show us what do you mean by (Fields)
Is it ListIcon fields or StringGadget or any other method ?
It will be very helpful for us

Re: Anyway to query hot keys?

Posted: Wed Dec 17, 2014 4:39 am
by jassing
RASHAD wrote:Can you submit a little example to show us what do you mean by (Fields)
Is it ListIcon fields or StringGadget or any other method ?
It will be very helpful for us
Anything... Actually; it shouldn't matter... you assign AddKeyboardShortcut() to a window, not a gadget, Just want to find out (if any) pre-exist so I can reset them once I am done.
Another way to think about it is I want to, while in 1 string gadget, temporarily override any existing keyboard shortcuts assigned to the window .

the specificness of it is a StringGadget, using EventType focus & lostfocus to set & reset the hot key

I can do a keyboard hook/callback; but I was just trying to make it as simple as possible for when I hand it off.

Here's just a quick idea

Code: Select all

OpenWindow(0,1,1,300,100,"")
StringGadget(0,1,1,50,20,"")
StringGadget(1,1,30,50,20,"")
AddKeyboardShortcut(0,#PB_Shortcut_A,0)

Declare StringGadget1()
Repeat
	Select WaitWindowEvent(100)
		Case #PB_Event_Gadget
			Select EventGadget()
				Case 1 : StringGadget1()
			EndSelect
		Case #PB_Event_Menu
			Select EventMenu()
				Case 0: Debug "for any other field"
				Case 1: Debug "Only for gadget #1"
			EndSelect
		Case #PB_Event_CloseWindow
			Break
	EndSelect
ForEver

; Imagine I know nothing about the above.... someone else wrote it and I can't change it or talk to them.

Procedure StringGadget1()
		Select EventType()
		Case #PB_EventType_Focus
			; here I want to query/store anything assigned to window #0, shortcut key #pb_shortcut_a so I can restore it later.
			AddKeyboardShortcut(0,#PB_Shortcut_A,1)
		Case #PB_EventType_LostFocus
			; Suppose I don't know anything above 5
			; here I want to restore the A to menu event 0.
	EndSelect
EndProcedure


Re: Anyway to query hot keys?

Posted: Wed Dec 17, 2014 12:03 pm
by PB Fanatic

Code: Select all

OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)

If RegisterHotKey_(WindowID(0),0,#MOD_ALT,#VK_F4)=0
  Debug "The Alt+F4 hotkey is in use"
EndIf

Re: Anyway to query hot keys?

Posted: Wed Dec 17, 2014 6:45 pm
by jassing
PB Fanatic wrote:

Code: Select all

OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)

If RegisterHotKey_(WindowID(0),0,#MOD_ALT,#VK_F4)=0
  Debug "The Alt+F4 hotkey is in use"
EndIf
Try it -- before calling RegisterHotKey_() issue:
AddKeyboardShortcut(#Window_0,#PB_Shortcut_Alt|#PB_Shortcut_F4,120)

When you run registerhotkey_() it returns 1
So, you don't know:
A) that was previously defined
B) that the previous shoftcut was set to menu item 120

Re: Anyway to query hot keys?

Posted: Thu Dec 18, 2014 9:18 am
by PB Fanatic
jassing wrote:Try it -- before calling RegisterHotKey_() issue:
AddKeyboardShortcut(#Window_0,#PB_Shortcut_Alt|#PB_Shortcut_F4,120)
When you run registerhotkey_() it returns 1

Code: Select all

OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)

AddKeyboardShortcut(0,#PB_Shortcut_Alt|#PB_Shortcut_F4,120)

result=RegisterHotKey_(WindowID(0),0,#MOD_ALT,#VK_F4)
Debug result ; 0

If result=0
  Debug "The Alt+F4 hotkey is in use"
EndIf
Debug shows 0 here, and the message, as expected.

Re: Anyway to query hot keys?

Posted: Thu Dec 18, 2014 6:37 pm
by missile69
The api function RegisterHotkey and PB's addkeyboardShortcut do two different things. You cannot use the success or failure of a call to RegisterHotkey to determine if the same keys were already used with AddKeyboardShorcut.

You can exclusively use RegisterHotkey instead of AddKeyboardShortcut and use code like DarkPlayer's to determine if a hotkey exists but since hotkeys are system-wide this can get messy. For example, the ability to register the hotkey Ctrl+D will vary from computer to computer because different software that may be installed might already be using it.

AddKeyboardShortcut creates a Keyboard Accelerator not a Hotkey. These shortcuts should work in your program no matter what. Your program has an accelerator table that stores these shortcuts. You can copy it with CopyAcceleratorTable. Once copied, iterate through the items in a loop to see if your shortcut already exists. You can change it if you like (still editing the copy), then destroy the old accelerator table and register the copy as a new one. See the msdn page here: http://msdn.microsoft.com/en-us/library ... itable_acc

Re: Anyway to query hot keys?

Posted: Thu Dec 18, 2014 7:41 pm
by jassing
Sorry for not using the proper term...
Is there a way to query pre-existing calls to AddKeyboardShortut() and determine what they are (menu #)?