Page 1 of 1
How to read keyboard keys into a window
Posted: Thu Jun 05, 2025 2:13 pm
by es_91
I try to create a CLI on Spider.
Is it possible to get the keys pushed from a "background" window (with a canvas on it), or do we need Screens/Sprite for this?
Re: How to read keyboard keys into a window
Posted: Thu Jun 05, 2025 9:03 pm
by Fred
Canvas should work, did you tried ?
Re: How to read keyboard keys into a window
Posted: Sun Jun 08, 2025 3:41 pm
by es_91
I remember failing with this idea years ago, so here i try again:
Code: Select all
global *window
global *canvas
procedure sizing ( )
resizeGadget ( *canvas,
0,
0,
windowWidth ( *window ),
windowHeight ( *window ) )
endprocedure
procedure canvas_event ( )
debug getGadgetAttribute ( *canvas,
#pb_canvas_input )
debug chr ( getGadgetAttribute ( *canvas,
#pb_canvas_key ) )
sizing ( )
endProcedure
*window = openWindow ( #pb_any,
0,
0,
0,
0,
#pb_compiler_filename,
#pb_window_background )
bindEvent ( #pb_event_sizeDesktop,
@ sizing ( ) )
bindEvent ( #pb_event_sizeWindow,
@ sizing ( ) )
*canvas = canvasGadget ( #pb_any,
0,
0,
0,
0,
#pb_canvas_keyboard )
bindEvent ( #pb_event_gadget,
@ canvas_event ( ) )
sizing ( )
I get 0 and "" only, but should not be any input when pressing a key? (I click the canvas twice so that it has focus, once is no enough)
Re: How to read keyboard keys into a window
Posted: Sun Jun 08, 2025 7:57 pm
by Caronte3D
Your code is... "strange" to me

Anyway I don't know if I understand you well.
I can get key inputs (if the canvas was focused) this way:
Code: Select all
Debug ""
Procedure Gadget_event()
If EventType()=#PB_EventType_Input
Debug Chr(GetGadgetAttribute(0, #PB_Canvas_Input))
EndIf
EndProcedure
OpenWindow(0, 0, 0, 100, 100, #PB_Compiler_Filename, #PB_Window_Background)
BindEvent(#PB_Event_Gadget, @Gadget_event())
CanvasGadget(0, 0, 0, 100, 100, #PB_Canvas_Keyboard|#PB_Canvas_DrawFocus)
SetActiveGadget(0)
Re: How to read keyboard keys into a window
Posted: Sun Jun 08, 2025 9:34 pm
by Peter
Caronte3D wrote: Sun Jun 08, 2025 7:57 pmYour code is... "strange" to me

this is the special "es_91"-Style.
Personally, I refuse to read this style far from any convention.
Re: How to read keyboard keys into a window
Posted: Tue Jun 10, 2025 10:57 pm
by es_91
Peter wrote: Sun Jun 08, 2025 9:34 pm
Caronte3D wrote: Sun Jun 08, 2025 7:57 pmYour code is... "strange" to me

this is the special "es_91"-Style.
Actually not, quite. I prefer ;{;} blocks around everything.
@Caronte3D: Thank you very much! It works with that little
'cause for some reason my program did never catch any focus on the canvas. I believe the PB doc does not state (in the canvas section) that the canvas shoud explicitly get the focus programatically.