GTK3 keyboard errors

Post your Gambas programming questions here.
Post Reply
rj71
Regular
Posts: 131
Joined: Thu Apr 13, 2023 6:39 pm
Location: Vancouver WA

GTK3 keyboard errors

Post by rj71 »

Hi All,

I asked this as a follow up question in my previous post but I figured I should start a new topic to get more eyes on it. I have a personal app where 100% of the user (me) input will be with a linux friendly remote I found on Amazon. Very similar to amazon's firestick remote. The remote control will be handled with keypress events. The code below I am trying to get some panels to slide in and out of view. It works great if I have it in a button. If I put that code in a keypress event I get "gb.gtk3: warning: calling the event loop during a keyboard event handler is ignored". The panels do move but I lose the sliding effect. I understand it is probably the Wait 0.1 that I have in there but if I remove the wait, then I lose my "animated" panel slide. Any suggestions on how to handle this? The remote control is essential for this project, if i can't use it then there is no point continuing because using a mouse would be silly for this.

Maybe there is something more I need to do with a keypress event?





dypan1.Y = dypan1.Y - 1
Wait 0.1
dypan1.Y = dypan1.Y - 2
Wait 0.1
dypan1.Y = dypan1.Y - 3
Wait 0.1
dypan1.Y = dypan1.Y - 4
Wait 0.1
dypan1.Y = dypan1.Y - 5
Wait 0.1
dypan1.Y = dypan1.Y - 6
Wait 0.1
dypan1.Y = dypan1.Y - 7
Wait 0.1
dypan1.Y = dypan1.Y - 8
Wait 0.1
dypan1.Y = dypan1.Y - 9
Wait 0.1
dypan1.Y = dypan1.Y - 10
Wait 0.1
dypan1.Y = dypan1.Y - 11
Wait 0.1
dypan1.Y = dypan1.Y - 12
Wait 0.1
dypan1.Y = dypan1.Y - 13
Wait 0.1
dypan1.Y = dypan1.Y - 14
Wait 0.1
dypan1.Y = dypan1.Y - 15
Wait 0.1

rj71
Regular
Posts: 131
Joined: Thu Apr 13, 2023 6:39 pm
Location: Vancouver WA

Re: GTK3 keyboard errors

Post by rj71 »

I am working an example project that I'll zip and post in a bit.
rj71
Regular
Posts: 131
Joined: Thu Apr 13, 2023 6:39 pm
Location: Vancouver WA

Re: GTK3 keyboard errors

Post by rj71 »

Here's a simple experiment I've been playing with.
You do not have the required permissions to view the files attached to this post.
BruceSteers
Legend
Posts: 2093
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: GTK3 keyboard errors

Post by BruceSteers »

It's because you use Wait.

Wait will allow the event loop to run and you should not do that inside a key event.

Solutions...

Make a Timer and have the key event start the timer.

Private $hTimer As Timer

Public Sub _New()

  $hTimer = New Timer As "MyTimer"
  $hTimer.Delay = 0

End

Public Sub Object_Keypress()

  If Key.Code = Key.Up Then   ' your proper key
    If Not $hTimer.Enabled Then $hTimer.Start  ' start the timer
 Endif

End

Public Sub MyTimer_Timer()

  $hTimer.Stop  ' stop the timer

  ' use Wait as much as you like now as we are not in the key event any more.

End




Or...

Maybe try using Sleep instead of Wait

Sleep will pause the program like Wait But not let the event loop run. (but then your panel movement code might not work)

You will be wise to make a switch that cannot be enabled while already enabled, or keyboard repeat might make your code behave badly.
rj71
Regular
Posts: 131
Joined: Thu Apr 13, 2023 6:39 pm
Location: Vancouver WA

Re: GTK3 keyboard errors

Post by rj71 »

BruceSteers wrote: Fri Mar 14, 2025 7:35 pm It's because you use Wait.

Wait will allow the event loop to run and you should not do that inside a key event.

Solutions...

Make a Timer and have the key event start the timer.

Private $hTimer As Timer

Public Sub _New()

  $hTimer = New Timer As "MyTimer"
  $hTimer.Delay = 0

End

Public Sub Object_Keypress()

  If Key.Code = Key.Up Then   ' your proper key
    If Not $hTimer.Enabled Then $hTimer.Start  ' start the timer
 Endif

End

Public Sub MyTimer_Timer()

  $hTimer.Stop  ' stop the timer

  ' use Wait as much as you like now as we are not in the key event any more.

End




Or...

Maybe try using Sleep instead of Wait

Sleep will pause the program like Wait But not let the event loop run. (but then your panel movement code might not work)

You will be wise to make a switch that cannot be enabled while already enabled, or keyboard repeat might make your code behave badly.
Thanks Bruce. I figure it was the Wait but wasn't sure how to handle that. The sleep deosn't work either...you're right, the panel slide won't work with that either. The timer idea does seem to work though and I'll continue down that path, thanks for that. Yeah, I'll implement some enable/disable routines...that experiment project was just a crude example.
User avatar
cogier
Site Admin
Posts: 1197
Joined: Wed Sep 21, 2016 2:22 pm
Location: Guernsey, Channel Islands

Re: GTK3 keyboard errors

Post by cogier »

I have got arrow keys working, as Bruce suggested, using a Timer. There is no need to stop and start the Timer as using Timer1.Trigger does the job. I have condensed your code considerably. Have a look here regarding the use of Quit to end programs.
slidingpanelsexperiment-CO-0.0.2.tar.gz
You do not have the required permissions to view the files attached to this post.
rj71
Regular
Posts: 131
Joined: Thu Apr 13, 2023 6:39 pm
Location: Vancouver WA

Re: GTK3 keyboard errors

Post by rj71 »

cogier wrote: Sat Mar 15, 2025 4:31 pm I have got arrow keys working, as Bruce suggested, using a Timer. There is no need to stop and start the Timer as using Timer1.Trigger does the job. I have condensed your code considerably. Have a look here regarding the use of Quit to end programs.

slidingpanelsexperiment-CO-0.0.2.tar.gz
Thanks cogier! I knew there had to be a way to condense that y axis move code I just haven't had time to experiment with it.
Post Reply