Clean way to setup hotkeys for button events?
-
- Enthusiast
- Posts: 480
- Joined: Thu Jul 27, 2006 4:06 am
Clean way to setup hotkeys for button events?
Hi, I'd like a clean way (windows only, if it requires callback I'm more than fine with that!) to setup hotkeys to my buttons.
I want to fire up a button event when I press enter for instance, if I have focus on a string gadget (The focus part, no problem) however I don't know how to nicely do this. I saw PB's go on hotkeys with the menu events, etc... I don't like it though!.
Also, I keep on getting the "ding" sound when I hit enter, could I disable this?
I want to fire up a button event when I press enter for instance, if I have focus on a string gadget (The focus part, no problem) however I don't know how to nicely do this. I saw PB's go on hotkeys with the menu events, etc... I don't like it though!.
Also, I keep on getting the "ding" sound when I hit enter, could I disable this?
Re: Clean way to setup hotkeys for button events?
Both questions have been answered several times before. Search the forums,
and read the FAQ post in the "Coding Questions" section. Thanks very much.
and read the FAQ post in the "Coding Questions" section. Thanks very much.
-
- Enthusiast
- Posts: 480
- Joined: Thu Jul 27, 2006 4:06 am
Thanks, but both solutions were not as elegant as I would of wanted, thats why I asked.
First of all, for the "ding" sound, you mention "#PB_String_Multiline|#ES_AUTOVSCROLL " yet... why would I even mix "pb's" constants with win api's?, that's not elegant! the day of tomorrow I won't know what pb's constant is, but sure I will about the win api one.
About the hotkey, the methods I've seen were about using PB's hotkey "solution" which is convoluted IMO and another one catching high-level "key strokes"... (I stopped searching right there).
Sorry to repeat but I just wanted a non convoluted solution... It's not my idea to annoy anyone.
First of all, for the "ding" sound, you mention "#PB_String_Multiline|#ES_AUTOVSCROLL " yet... why would I even mix "pb's" constants with win api's?, that's not elegant! the day of tomorrow I won't know what pb's constant is, but sure I will about the win api one.
About the hotkey, the methods I've seen were about using PB's hotkey "solution" which is convoluted IMO and another one catching high-level "key strokes"... (I stopped searching right there).
Sorry to repeat but I just wanted a non convoluted solution... It's not my idea to annoy anyone.
> why would I even mix "pb's" constants with win api's?, that's not elegant!
Well, you did say: "I'd like a clean way (windows only, if it requires
callback I'm more than fine with that". So what's wrong with mixing
API then?
As for hotkeys activating when Enter is pressed on a StringGadget, you
can just check for a gadget event on the gadget and use EventWParam()
to see if it's #PB_Key_Enter.
As for the "ding" sound: it's explained in the link from the FAQ that this is
normal because you can't use Enter on a single-line StringGadget, and so
Windows does the "ding" to tell you that the key is invalid there.
Well, you did say: "I'd like a clean way (windows only, if it requires
callback I'm more than fine with that". So what's wrong with mixing
API then?
As for hotkeys activating when Enter is pressed on a StringGadget, you
can just check for a gadget event on the gadget and use EventWParam()
to see if it's #PB_Key_Enter.
As for the "ding" sound: it's explained in the link from the FAQ that this is
normal because you can't use Enter on a single-line StringGadget, and so
Windows does the "ding" to tell you that the key is invalid there.
*** Edited to remove global var ***
Not fully tested and not sure if it meets your standards for elegance...
Not fully tested and not sure if it meets your standards for elegance...
Code: Select all
Procedure SringProc(hwnd, msg, wParam, lParam)
oldproc = GetProp_(hwnd, "_oldProc")
Select msg
Case #WM_CHAR
;...Remove ding sound and press button
If wParam = #VK_RETURN
SendMessage_(GadgetID(1), #BM_CLICK, 0, 0)
ProcedureReturn 0
EndIf
EndSelect
ProcedureReturn CallWindowProc_(oldproc, hwnd, msg, wParam, lParam)
EndProcedure
If OpenWindow(0, 0, 0, 300, 200, "Silent StringGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
StringGadget(0, 10, 10, 280, 25, "Normal StringGadget")
ButtonGadget(1, 10, 40, 280, 25, "Click me or press Enter in StringGadget")
EditorGadget(2, 10, 70, 280, 125)
oldproc = GetWindowLong_(GadgetID(0), #GWL_WNDPROC)
SetProp_(GadgetID(0), "_oldproc", oldproc)
SetWindowLong_(GadgetID(0), #GWL_WNDPROC, @SringProc())
Repeat
event = WaitWindowEvent()
If event = #PB_Event_Gadget And EventGadget() = 1
AddGadgetItem(2, -1, "Current string text is: " + GetGadgetText(0))
EndIf
Until event = #PB_Event_CloseWindow
RemoveProp_(GadgetID(0), "_oldproc")
EndIf
Last edited by Sparkie on Sun Dec 16, 2007 3:22 am, edited 1 time in total.
What goes around comes around.
PB 5.21 LTS (x86) - Windows 8.1
PB 5.21 LTS (x86) - Windows 8.1
This doesn't ding on my system (Vista):
npath
Code: Select all
Declare main()
Declare buttonClickEvent()
main()
Procedure main()
If OpenWindow(0, 0, 0, 170, 80, "Main", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
If CreateGadgetList(WindowID(0))
StringGadget(1, 10, 10, 150, 25, "")
ButtonGadget(2, 10, 40, 150, 25, "OK")
EndIf
AddKeyboardShortcut(0, #PB_Shortcut_Return, 1)
SetActiveGadget(1)
EndIf
Repeat
event.l = WaitWindowEvent()
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case 2
buttonClickEvent()
EndSelect
Case #PB_Event_Menu
Select EventMenu()
Case 1
buttonClickEvent()
EndSelect
EndSelect
Until (event = #PB_Event_CloseWindow)
EndProcedure
Procedure buttonClickEvent()
SetWindowTitle(0, GetGadgetText(1))
EndProcedure
> This doesn't ding on my system (Vista)
@npath: The trouble with using AddKeyboardShortcut() is that it effectively
disables the Enter key for your app. For example: add an EditorGadget()
to your app and you won't be able to press Enter in it (without writing the
extra code to handle the keypress yourself).
@npath: The trouble with using AddKeyboardShortcut() is that it effectively
disables the Enter key for your app. For example: add an EditorGadget()
to your app and you won't be able to press Enter in it (without writing the
extra code to handle the keypress yourself).
Very slight modification of Sparkie's excellent code, which prevents the string gadget from losing focus (i.e., added SetActiveGadget(0) to the gadget event):
npath
Code: Select all
Global oldproc
Procedure SringProc(hwnd, msg, wParam, lParam)
Select msg
Case #WM_CHAR
;...Remove ding sound and press button
If wParam = #VK_RETURN
SendMessage_(GadgetID(1), #BM_CLICK, 0, 0)
ProcedureReturn 0
EndIf
EndSelect
ProcedureReturn CallWindowProc_(oldproc, hwnd, msg, wParam, lParam)
EndProcedure
If OpenWindow(0, 0, 0, 300, 200, "Silent StringGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
StringGadget(0, 10, 10, 280, 25, "Normal StringGadget")
ButtonGadget(1, 10, 40, 280, 25, "Click me or press Enter in StringGadget")
EditorGadget(2, 10, 70, 280, 125)
oldproc = GetWindowLong_(GadgetID(0), #GWL_WNDPROC)
SetWindowLong_(GadgetID(0), #GWL_WNDPROC, @SringProc())
Repeat
event = WaitWindowEvent()
If event = #PB_Event_Gadget And EventGadget() = 1
AddGadgetItem(2, -1, "Current string text is: " + GetGadgetText(0))
SetActiveGadget(0)
EndIf
Until event = #PB_Event_CloseWindow
EndIf
-
- Enthusiast
- Posts: 480
- Joined: Thu Jul 27, 2006 4:06 am
Even though I haven't compiled the code (no pb in blackberry yet!) for what I could read it seems like a nice solution to me ie. windows message based which is perfect the only bitter part is the global var! I'll see if I can use user data on the string "gadget" or something within the lines...
Thanks
Thanks
-
- Enthusiast
- Posts: 480
- Joined: Thu Jul 27, 2006 4:06 am