long Keypress

Post

Posted
Rating:
#1 (In Topic #2035)
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
Hi folks,

I am currently creating a game from the 80s (1978), Space Invaders ("older" people know this of sure 😁).

The first steps have been taken, and some functionality is already in place.

One problem is pressing the left and right keys for moving the defender.
When pressed individually, everything works fine.
When the key is held down, it takes too long for the repeat function to kick in. So the Aliens
shoot me down very often because of this delay.

What I tried without success:
if the key is pressed (left or right in this case) the _keypress() sub is called.
With a public var I set to true in this case I call the moveit sub.
In there I check if the var is true or false.

If true it repeats moving, till the var is set to false in the _keyrelease() event.

Somehow the hole app freezes… ( maybe because of endless calls to _keypress() )

Has anyone an idea to get this running?

I don't want to change the system accessibility values if possible (or If we have to we set the values
back to the default values at the end of the game)

Also what I never encountered before:
I gave the form background color to black, so the menu also gets black, I just did a foreground colour
for better sight, any chance to set the background colour of the menu independently?

Image

Invader.jpg

Invader.jpg


And the last one:
in the keypress sub the key.code for ctrl is not recognised, so I used the key.code I got  as a number,
ctrl is for the shooting,
(Documentation: NEVER use the key values directly, as they change between GUI components. Always use these constants! )

Thanks,
Yogi

 
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

Yogi said

I am currently creating a game from the 80s (1978), Space Invaders ("older" people know this of sure 😁).

Played that a lot when I was young. Great game. Been thinking often to create this myself, but just too busy with other stuff.
Good to see soemone else on it :thumbs:

Yogi said

One problem is pressing the left and right keys for moving the defender.
When pressed individually, everything works fine.
When the key is held down, it takes too long for the repeat function to kick in. So the Aliens
shoot me down very often because of this delay.

Change the delay on your system should fix that

Yogi said

I don't want to change the system accessibility values if possible (or If we have to we set the values<br />
back to the default values at the end of the game)

Ok, so change the delay when starting the game from within the game and set it back when closing the game, from within the game.
You will need to dive into where this configuration info is saved and modify it on the fly with some code in your game..
So, some research into how your distro takes care of that will be required and where all this info is stored.
As this is user based, you should not require elevated rights to change this from code.

Yogi said

Also what I never encountered before:
I gave the form background color to black, so the menu also gets black, I just did a foreground colour
for better sight, any chance to set the background colour of the menu independently?
]

These things (customise colors) are all very toolkit related and a pain in the ass. There is no one good solutions.
Did you try setting the form arrangement to fill and add a panel set to expand with a black background and use that as form background instead. That should not interfere with the form menu.

Edit: With that I mean. Select ALL controls on the form. CTRL + X (Cut), draw a Panel on the form, select the Panel and CTLR + V (Paste) all controls into the Panel.
Clear property Background on the form. Set form property Arrangement to Fill and Panel property Expand to True. Change Panel Background to black.
Run and see what happens… :thumbs:

Yogi said

And the last one:
in the keypress sub the key.code for ctrl is not recognised, so I used the key.code I got  as a number,
ctrl is for the shooting,
(Documentation: NEVER use the key values directly, as they change between GUI components. Always use these constants! )
 

On above and the freeze: Do you have a excerp of the code that manages the keys.
Hard to tell what is happening without seeing any code.

Looking forward to play this one  :thumbs:

Last edit: by gbWilly


gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’

Yogi said

One problem is pressing the left and right keys for moving the defender.
When pressed individually, everything works fine.
When the key is held down, it takes too long for the repeat function to kick in. So the Aliens
shoot me down very often because of this delay.

Can you post some code so we can see what's happening. Or all of it, it looks cool!
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#4
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
Dear gbWilly ,

Thanks a lot for your detailed replay and help 👍

I found out how to change the keyboard delay in the Ubuntu GUI, also from the Terminal with

Code (gambas)

Arr=["gsettings", "set", "org.gnome.desktop.peripherals.keyboard", "delay", "50"]
Exec Arr
but for the moment other things are more important - but, if the delay is less everything is working well.
(so you have been right, it solves the problem!)

Regarding the Container, thanks for the detailed description, maybe it is also possible to select all,
right click and select embed in a container.

The freezing thing is obsolet now, because the delay if correctly set does the trick.

Thanks cogier for helping out,

The keypress with control, I managed to solve it, before it was:

Code (gambas)

[..]
  Else If Key.Code = Key.Left Then
    moveDefender("left")
  Else If Key.Code = Key.Right Then
    moveDefender("right")
  Else If Key.Code = 65507 Then 'ctrl Shoot
    moveDefender("shoot")
  Endif

after, the working solution:

Code (gambas)

[..]
  Else If Key.Code = Key.Left Then
    moveDefender("left")
  Else If Key.Code = Key.Right Then
    moveDefender("right")
  Else If Key.Control Then 'ctrl as Shoot
    moveDefender("shoot")
  Endif
 

A little more work and I'll post a pre version,
Kind regards,
Yogi
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
Hi,

Here is the current source, after giving a container to the form the keypress events are no longer working.

Public Sub Form_KeyPress()

and then I added an event to the panel where all the other objects are in,
also not working!

Public Sub PanelBack_KeyPress()

No keypress… 🤮


If you are in the same line as the defender, left mouse click is shooting, right mouse click moving,
but only in the same line as the defender!

It is not the copy of the original even if the invaders should be similar.
The shooting defense allows up to 3 shots visible on the screen, that is much
more than the original, and the buildings are bombproof 😁

Attachment

spaceinvaders-0.0.1.tar.gz

Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
Here I went back to the previous version without container, just the main form.

I don't know if this happens to you too, sometimes in the beginning the keypresses are not recognized - even the ESC key.
But after a while shooting with the left mouse button, it works again 🤷‍♀️

Attachment

spaceinvaders-0.0.2.tar.gz

Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
I'm on Gambas 3.21.3 if it is important.

In this game I used 4 timers, I thought it would be more fluent if you have more independent "Threads".

Actual I'm working with 2 timers, calling different subs. Seems to work also fluent.

But the main problem stays, at the moment I can neither fire nor move till the 1st stage cleared,
no keypress event fires.
After that, it works again. Frustrating not to see the tree in the wood….

Any help is greatly appreciated.

In the virtualbox with GambOS Gambas 3.19.6 I tried the project, but it shows this:
screenshot1.jpg

Last edit: by Yogi

Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
So, I nailed it somehow down, deactivated the timers, still no keypress detected,
debugged with F8 - and see, it is working from the beginning after last command awaiting keypress
and mouse (timers still deactivated!).

After form_open() the keypress event is working.
If pressing the button "Start Game" no keyevent is triggered.

if I make a breakpoint F9 at the last END command after creating the scene
and press F5 to continue, it works! Repeatedly tested!

I even did a PRINT for the console to see when a keypress is triggered, it works
only as described above.

GDB Debugger doesn't give me a clue:

Code

[New Thread 0x7fffe26b66c0 (LWP 19292)]                                         
[New Thread 0x7fffe1eb56c0 (LWP 19293)]
[New Thread 0x7fffe16b46c0 (LWP 19294)]
[Thread 0x7fffe26b66c0 (LWP 19292) exited]
[Thread 0x7fffe1eb56c0 (LWP 19293) exited]
Taste gedrückt: 02/03/2026 09:52:02.563/65363
Taste gedrückt: 02/03/2026 09:52:03.061/65363
Taste gedrückt: 02/03/2026 09:52:03.092/65363
Taste gedrückt: 02/03/2026 09:52:03.122/65363
Taste gedrückt: 02/03/2026 09:52:03.153/65363
Taste gedrückt: 02/03/2026 09:52:03.184/65363
Taste gedrückt: 02/03/2026 09:52:03.214/65363
Taste gedrückt: 02/03/2026 09:52:03.245/65363
Taste gedrückt: 02/03/2026 09:52:03.275/65363
Taste gedrückt: 02/03/2026 09:52:03.305/65363
Taste gedrückt: 02/03/2026 09:52:03.336/65363
Taste gedrückt: 02/03/2026 09:52:03.367/65363
After Build Aliens and STarted: 02/03/2026 09:52:06.705
[Thread 0x7fffe16b46c0 (LWP 19294) exited]
[Thread 0x7fffe2f606c0 (LWP 19291) exited]
[Thread 0x7fffe3fff6c0 (LWP 19289) exited]
[Thread 0x7ffff12b96c0 (LWP 19287) exited]
[Thread 0x7ffff1aba6c0 (LWP 19286) exited]
[Thread 0x7ffff7eabb80 (LWP 19278) exited]
[Thread 0x7ffff0ab86c0 (LWP 19288) exited]
[New process 19278]
[Inferior 1 (process 19278) exited normally]
(gdb)
(gdb) bt
No stack.
(gdb)


I had a look after "Search the Gambas Mailinglists"
There Benoit stated:

Code

There is no interface in the QT component to globally intercept key events
yet, but there is a trick:

If you have a menu in your form, its keyboard shortcuts are globally managed.
So you can add an hidden menu to your form to solve your problem.

Regards,

--
Benoit Minisini

I added a menu with shortcut "F1", once pressed after Scene created, all keys are working again!
The sub called with F1 just shows "balloon.info()"

Where is the culprit?

Solution found

it seems somehow the mainform loses focus during the creation of the scene.
If I put a last command after creation

Me.SetFocus

all is well and working


Last edit: by Yogi

Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by Yogi)
#9
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

Yogi said

In the virtualbox with GambOS Gambas 3.19.6 I tried the project, but it shows this:
screenshot1.jpg
You have 3.21.3 bytecode (your source).
So, on 3.19.6 you FIRST DO COMPILE ALL.
That will give you the 3.19.6 bytecode and it should now work fine.
It runs on my 3.19.6 version (same as the one on GambOS).


And when it come to these kind of problems, the order in wich things trigger can be of influence, what has focus can be of influence, a lot can be of influence.
The debugger is not helpful for this, but the Debug instruction could be (a print to console with location in code included). See here on the wiki

To track stuff that makes no sense, I sometimes place an instruction like, Debug "Name of event" at the start of each events (replace Name of event with the event name you placed the Debug code in).
Next, I run the code to see in what order things happen and managed to solve issues by simply moving code to an earlier or later event, because the code was fine, the moment it got seen and triggered wasn't.

But you seem to have solved it :thumbs:


 

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#10
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’

gbWilly said

You have 3.21.3 bytecode (your source).
So, on 3.19.6 you FIRST DO COMPILE ALL.
That will give you the 3.19.6 bytecode and it should now work fine.
It runs on my 3.19.6 version (same as the one on GambOS).



 

gbWilly
Thanks Dear Willy, the compile all did the job, it works now there also 👍
Online now: No Back to the top

Post

Posted
Rating:
#11
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
Hi folks,

I'm struggling with the special effects, sound especially.
It becomes not fluent the more sound I put in the game via Mediaplayer.
No matter what I've tried, with more timers, etc., it doesn't run smoothly as soon as I play sound files.
If anyone has any ideas, let me know.

There are 2 versions in this source, FMain1 and FMain2, if you try both as starting class you know what I mean.

Here we go:
Attachment

spaceinvaders-0.0.3.tar.gz

Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

Yogi said

Hi folks,

I'm struggling with the special effects, sound especially.
It becomes not fluent the more sound I put in the game via Mediaplayer.
I think the timers might be causing delays Adding more won't help.
They might also interfere with you key presses.

Have you tried my Debug suggestion as it will reveal to you how often and in what order all is triggered and will illuminate your comprehension of what is really going on.
You will be surprised of what is going on, make sure to place them on all events.

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#13
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
gbWilly
Dear Willy,
Always a pleasure for me to read your suggestions, they help in going deeper into Gambas.

Yes I did the debug suggestion, but not that much as it have should.
From this early morning till now I did a deeper research with the Debug and what I learned
about the timers, wow, they are really precise. I worked all the subs with the timers, and what
I could do because of the timers - I removed some. For most purposes it should be enough to
have one timer and from the sub event of this timer call the other subs needed.

So what I could find out, the timers are ok, also my subs hopefully ok - I couldn't find anything
in the source delaying much so that it is not fluent.
Also one thing what disturbed me was the ctrl key, if pressed once, it triggered mostly twice in the
_KeyPress() sub

So it came to the next step, I changed the Project from gb.gui to gb.qt5 - and - tatah
the hole thing works like a charm - including no double trigger from the ctrl key.
Just seeing that I have to adjust the graphical elements.

What is your suggestion regarding the GUI component for use?

Thanks a lot,
Kind regards,
Yogi
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by Yogi)
#14
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
I have looked at FMain2 > Timersnds. I can't see why it is there. If you want the sound, just place the code in the main routine. Or am I missing something?

The routine below has an Integer created, but it only counts to 20, a byte would be faster.

Code

Public Sub createAlien(ByRef aPict As PictureBox[], oPanel As Panel, oPict As Picture)
Dim i As Integer

The same here as i only counts to 4

Code

Public Sub createBase()
Dim i, x As Short

You could also consider using Task, which will allow the code to run in its own thread.
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by Yogi)
#15
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
Here is a possible alternative for moving the 'Defender'

Attachment

MoveDefender-0.1.tar.gz

Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by Yogi)
#16
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

Yogi said

@gbWilly

What is your suggestion regarding the GUI component for use?
My suggestion is use the component that works best for you under your circumstances.
I use qt5 as stuff just works better even though I run a gtk based desktop (Mate).

In upcoming Gambas Book 2, chapter 1 write on the matter of GUI components, toolkits, x11 and Wayland.
A little sneak preview in screenshot below on what I advise on GUI to use.


post.png

It's important background info to be aware off, as I see a lot of problems posted here are related to either one of these (GUI/Toolkit and X11/Wayland) and can be easily prevented when you know what you are dealing with.
Book 2 is in testing now, so a little patience is needed to read it all, or maybe apply to become part of the contributor team and become a guide tester as well , so you have early access to the Book for testing purposes (meaning study it, try it and report issues etc) ;)


Fun stuff, using Debug, to see how all is going under the hood, often very insightful. Also a tip from Book 2 :thumbs:

Enjoy…:thumbs:


 

Last edit: by gbWilly


gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#17
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

Yogi said

Always a pleasure for me to read your suggestions, they help in going deeper into Gambas.
It's comparable to:

I could provide you with the food
OR
I could point out to you how to grow your own food.

What is the best form of help, making you dependend or independed?
My preferred approach is the latter, and I'm glad you like it.

It leaves room for self discovery, and growth of awareness on matters.
It's the best school there is when it comes to gaining insight… 

No book that can beat that, not even mine… :lol:

Enjoy… :thumbs:

 

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#18
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’

cogier said

I have looked at FMain2 > Timersnds. I can't see why it is there. If you want the sound, just place the code in the main routine. Or am I missing something?

The routine below has an Integer created, but it only counts to 20, a byte would be faster.

Code

Public Sub createAlien(ByRef aPict As PictureBox[], oPanel As Panel, oPict As Picture)
Dim i As Integer

The same here as i only counts to 4

Code

Public Sub createBase()
Dim i, x As Short

You could also consider using Task, which will allow the code to run in its own thread.


Dear cogier ,
As well as Willy your are one you can depend on.

Changed your suggestions.
Regarding the sound I had the feeling that it consumes time, but there I'm wrong.
Will see how it works out.

As I found out today about the timers, task will be my last try, but thanks for the hint!

Kind regards,
Yogi
Online now: No Back to the top

Post

Posted
Rating:
#19
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’

cogier said

Here is a possible alternative for moving the 'Defender'

Attachment

MoveDefender-0.1.tar.gz


I like this thinking outside the box 👍
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#20
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’

gbWilly said

Yogi said

Always a pleasure for me to read your suggestions, they help in going deeper into Gambas.
It's comparable to:

I could provide you with the food
OR
I could point out to you how to grow your own food.

What is the best form of help, making you dependend or independed?
My preferred approach is the latter, and I'm glad you like it.

It leaves room for self discovery, and growth of awareness on matters.
It's the best school there is when it comes to gaining insight… 

No book that can beat that, not even mine… :lol:

Enjoy… :thumbs:

 

That's pretty much the same approach I take when someone asks me about Yoga.
There isn't just one right way; people are different and everyone has to find their
own path - but you can help a little.

Kind regards,
Yogi
Online now: No Back to the top
1 guest and 0 members have just viewed this.