Hi all,
Been sick the past few days, decided to take a break from my video app, and try and skeleton a basic GUI for that Text RPG I've been planning. But then I kind of ran into a hitch I didn't expect. Trapping key press events, in a way that is preferably cross-platform (I'd like to potentially release the game for both Windows and Linux), and doesn't interfere with normal operation of OS functions like ALT+TAB switching, or other functions within the games GUI window in general..
Some forum searches got me lots of lines of codes, and various methods, as well as strong opinions; but I think it left me more uncertain!
The Keyboard library doesn't seem much use in this case, as it seems to be intended for console applications, or 2D apps using an open "Screen".
I'm not sure Shortcutkeys are want I want at all, although I could use them for some functionality such as firing macros... I am after a certain functionality in particular, however.
Here is a basic outline of some functionality I was thinking of.
For the very basics, I have an editor gadget that will function as the main display window (read only), and then a StringGadget which I will use for user input. I would like to trap the Enter key in this case, so that when the user enters some text, it sets off an event that will grab that text into a variable, clear the input box, and pass the text in the variable on to the command parser.. A rather typical behavior I suppose.
I also like the behavior most applications have, where if you hit Enter when you're in a subwindow/menu etc.. it defaults as the "user has hit OK button" action, closes that menu, etc.. I've seen one example of using the keyboard to navigate a GUI and set off button presses, but it looked pretty long and a bit intimidating/complicated.
However in terms of "where to start", I am looking for some basic advice on things I should investigate/research.. If anyone wants to provide a basic event loop sample, that would probably help a little.. But also keep in mind, I am looking for something that can work on both Windows & Linux, without any major hiccups / hacks needed..
I thought about using SDL.. but it looks like for Windows I'd have to go through the annoyances of downloading SDL and then wrapping it with prototypes, etc.. Kind of a shame because from what I read PB uses it natively for Linux compiles.. But it provides low level access to so much stuff it seems like a really useful too, even if you don't need it for its graphics rendering purposes.
Anyway.. Advice/examples/pointers in the right direction are all appreciated..
Best approach for keyboard events...
Re: Best approach for keyboard events...
using AddKeyboardShortcut() is the way to go, you can then use a select to find the Active gadget and do whatever
Windows 11, Manjaro, Raspberry Pi OS


-
- Addict
- Posts: 1675
- Joined: Sun Dec 12, 2010 12:36 am
- Location: Somewhere in the midwest
- Contact:
Re: Best approach for keyboard events...
I had a feeling someone was gonna say that..
I will start working on that then.. Just seemed unsure as apparently some people believe its not the "proper" way.
But as long as I do it properly, it should be cross platform and won't intefere with ALT+Tab operations, I am guessing.
I will start working on that then.. Just seemed unsure as apparently some people believe its not the "proper" way.
But as long as I do it properly, it should be cross platform and won't intefere with ALT+Tab operations, I am guessing.
Re: Best approach for keyboard events...
in case you hadn't figured it out already, i made a small example for ya
Code: Select all
OpenWindow(0, 350, 195, 199, 238, "Example", #PB_Window_SystemMenu)
Frame3DGadget(#PB_Any, 10, 10, 180, 160, "Commands")
cmdoutput=ListViewGadget(#PB_Any, 20, 30, 160, 130)
cmdinput=StringGadget(#PB_Any, 10, 180, 180, 20, "")
cmdsend=ButtonGadget(#PB_Any, 10, 210, 180, 20, "press Me or Enter to send command")
CreateMenu(0,WindowID(0))
MenuItem(1,"Hidden Menu Item For Shortcut")
HideMenu(0,1)
AddKeyboardShortcut(0,#PB_Shortcut_Return,1)
Repeat
e=WaitWindowEvent(5)
Select e
Case 0
Delay(5)
Case #PB_Event_Gadget
If EventGadget() = cmdsend
txt.s=GetGadgetText(cmdinput)
If txt
AddGadgetItem(cmdoutput,-1,txt)
SetGadgetText(cmdinput,"")
SetActiveGadget(cmdinput)
EndIf
EndIf
Case #PB_Event_Menu
If EventMenu() = 1
If GetActiveGadget() = cmdinput
txt.s=GetGadgetText(cmdinput)
If txt
AddGadgetItem(cmdoutput,-1,txt)
SetGadgetText(cmdinput,"")
EndIf
EndIf
EndIf
EndSelect
Until e=#PB_Event_CloseWindow
Re: Best approach for keyboard events...
Here is a variation that eliminates some duplicated code in the event loop and avoids a hidden menu.
Code: Select all
Enumeration
#winMain
#cmdOutput
#cmdInput
#cmdSend
EndEnumeration
OpenWindow(#winMain, 350, 195, 199, 238, "Example", #PB_Window_SystemMenu)
Frame3DGadget(#PB_Any, 10, 10, 180, 160, "Commands")
ListViewGadget(#cmdOutput, 20, 30, 160, 130)
StringGadget(#cmdInput, 10, 180, 180, 20, "")
ButtonGadget(#cmdSend, 10, 210, 180, 20, "Press Me or Enter to send command") ; Cannot use #PB_Any as ...
AddKeyboardShortcut(0, #PB_Shortcut_Return, #cmdSend) ; Last parameter must be 0..64000
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget, #PB_Event_Menu
Select EventGadget() ; Or EventMenu()
Case #cmdSend
txt.s=GetGadgetText(#cmdInput)
If txt
AddGadgetItem(#cmdOutput,-1,txt)
SetGadgetText(#cmdInput,"")
SetActiveGadget(#cmdInput)
EndIf
EndSelect
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
Anthony Jordan
-
- Addict
- Posts: 1675
- Joined: Sun Dec 12, 2010 12:36 am
- Location: Somewhere in the midwest
- Contact:
Re: Best approach for keyboard events...
Hi,
Sorry I totally forgot about this thread! I became so engrossed in my search for a Text box that will let me use colors how I want, I haven't done much else.. Well, ok I've also been playing the English translation of Ys IV: The Ark of Napishtim
Thanks for the code, I know it will come in handy when I am ready to do this part of my GUI.
Sorry I totally forgot about this thread! I became so engrossed in my search for a Text box that will let me use colors how I want, I haven't done much else.. Well, ok I've also been playing the English translation of Ys IV: The Ark of Napishtim
Thanks for the code, I know it will come in handy when I am ready to do this part of my GUI.