How to read keyboard keys into a window

Just starting out? Need help? Post your questions and find answers here.
es_91
Posts: 56
Joined: Sat Aug 29, 2015 10:25 pm

How to read keyboard keys into a window

Post 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?
Fred
Site Admin
Posts: 1820
Joined: Mon Feb 24, 2014 10:51 am

Re: How to read keyboard keys into a window

Post by Fred »

Canvas should work, did you tried ?
es_91
Posts: 56
Joined: Sat Aug 29, 2015 10:25 pm

Re: How to read keyboard keys into a window

Post 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)
User avatar
Caronte3D
Posts: 187
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: How to read keyboard keys into a window

Post 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)
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How to read keyboard keys into a window

Post 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.
es_91
Posts: 56
Joined: Sat Aug 29, 2015 10:25 pm

Re: How to read keyboard keys into a window

Post 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

Code: Select all

SetActiveGadget (#gadget)
'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.
Post Reply