How to "release" keydown event?

Hi everyone, how are you?

I’m making a game and I have a question.

The game is controlled via keyDown on MacOS. The arrow keys control movement. The problem I’m having is that when I press and hold the key, the game stops working until I release the key. How can I make it so that there is an internal interval in the keydown event, so that other processes continue to run, even with keydown pressed?

Thanks!

Alex

Show us the code in your KeyDown event.

Select Case key.Asc
Case 30
  c_game.fly.Move("UP", c_game)
Case 29
  c_game.fly.Move("RIGHT", c_game)
Case 31
  c_game.fly.Move("DOWN", c_game)
Case 28
  c_game.fly.Move("LEFT", c_game)
End Select

If myCanvas<>Nil Then 
  myCanvas.Refresh
End If

Return True

i would use the global keyboad status. Seems u are using a keydown event in a canvas?

If Keyboard.AsynckeyDown(&h00) Then
' do something with this key here
End If
1 Like

Use Markus’ code in a timer and you’ll get what you want.

no, i’m using it on the window (the same where the canvas is)

i tried but it’s not working. i’ll give more details:

wdwGame
— event
— — keydown

// game starts here, after first keydown
// will be changed in the future
GameTimer.Mode = Timer.ModeMultiple

Select Case key.Asc
Case 30
  c_game.mm.Move("UP", c_game)
Case 29
  c_game.mm.Move("RIGHT", c_game)
Case 31
  c_game.mm.Move("DOWN", c_game)
Case 28
  c_game.mm.Move("LEFT", c_game)
End Select

If myCanvas<>Nil Then 
  myCanvas.Refresh
End If

Return True

controls
— gametimer
— — action

// apago o caminho para que o algoritmo crie um novo!!!
c_queue.items.RemoveAll 

Var algorithm As myAlgo = New myAlgo2025
algorithm.MovePetGame(c_game)

myCanvas.Refresh

— myCanvas
— — paint

If levelLoaded Then
  
  // limpo o fundo
  g.DrawingColor = gameBackgroudColor
  g.FillRectangle(0, 0, g.Width, g.Height)
  
 // … etc, etc …
  
End If

i tried but it’s not working.

generally try to follow what your game/events do with debug output.

Function KeyDown(key As String) Handles KeyDown as Boolean
  System.DebugLog CurrentMethodName
  
  Select Case key.Asc
  Case 30
    System.DebugLog("UP")
  Case 29
    System.DebugLog("RIGHT")
  Case 31
    System.DebugLog("DOWN")
  Case 28
    System.DebugLog("LEFT")
  End Select
  
End Function

your canvas paint event could look like this.
means this game class have methods with g as graphics argument.

Sub Paint(g As Graphics, areas() As Rect) Handles Paint
  
  Game.PaintBackground(g)
  Game.PaintFigures(g)
  
End Sub

i’m stuck on this.

all game works fine. if i press any key to move mypet (and it moves fine), the other pets stop moving.

fixed. i asked claude.ai

before…
GameTimer.RunMode = Timer.RunModes.Multiple

This means that every time a key is pressed, the timer is reset, which may be causing interference with the cat’s continuous movement.

now…
If GameTimer.RunMode = Timer.RunModes.Off Then
GameTimer.RunMode = Timer.RunModes.Multiple
End If

so obvious

1 Like

yes :slight_smile: as i saw your example code i asked myself why are you setting the timer mode there.

its still the wrong place.

try using a enumeration for your game/pet/player status or a flag as condition.

games usually have minimum 2 running timers, one for the update and one for the drawing.
as example physics run 1000 times per second and the drawing 60 fps. (parallel)

yes, i have another timer and i already removed from here!

thank you.