locked
A very Strange thing about "lastkey" RRS feed

  • Question

  • Here is a problem to which I found absolutely no answer. And I don't thinks there is something wrong with my code. But if you find the problem please let me know. Here is the problem:

    I was trying to develop a 2-player game in which each player controls a vehicle and moves around the screen using keyboard. As long as each individual moves independently everything was ok. But the  only shocking problem is that under very special cases one of the players cannot move to one direction. for example when player 2 is moving towards bottom right, player 1 can move to any direction except top left!!!! This was super strange to me. There are also other exceptional conditions.

    Here is the code:

    GraphicsWindow.KeyDown = keydown
    GraphicsWindow.KeyUp = keyup
    GraphicsWindow.BackgroundColor = "lightblue"
    GraphicsWindow.Top = "0"
    GraphicsWindow.Height = 600
    GraphicsWindow.Width = 900
    LDPhysics.SetGravity(0, 0)
    x = 300
    y = 450
    a = 0
    
    x2 = x
    y2 = y
    a2 = a
    
    car1 = ImageList.LoadImage(Program.Directory + "/pics/tank1.png")
    car1 = Shapes.AddImage(car1)
    
    car2 = ImageList.LoadImage(Program.Directory + "/pics/tank2.png")
    car2 = Shapes.AddImage(car2)
    
    mass = LDPhysics.GetMass(car1)
    LDPhysics.AddMovingShape(car1, 1, 0, mass)
    LDPhysics.SetPosition(car1, x, y, a)
    
    LDPhysics.AddMovingShape(car2, 1, 0, mass)
    LDPhysics.SetPosition(car2, x2, y2, a2)
    
    
    
    
    
    
    While ("True")
      car2move()  
      car1move()
    
    
    LDPhysics.DoTimestep()
    Program.Delay(20)
    
    EndWhile
    
    
    
    
    
    Sub keyup
      k = GraphicsWindow.LastKey
      If (k="Right") Then
      right = 0
    ElseIf (k = "Left") then
      left = 0
      elseif (k = "Up") then
      up = 0
    elseif (k = "Down") then
      down = 0
      EndIf
      
      
      
      
      
      
    'k2 = GraphicsWindow.LastKey
      If (k = "D") Then
      right2 = 0
    ElseIf (k = "A") then
      left2 = 0
      elseif (k = "W") then
      up2 = 0
    elseif (k = "S") then
      down2 = 0
    EndIf
    
    EndSub
    
    Sub keydown
      k = GraphicsWindow.LastKey
      If (k="Right") Then
      right = 1
    ElseIf (k = "Left") then
      left = 1
      elseif (k = "Up") then
      up = 1
    elseif (k = "Down") then
      down = 1
    EndIf
    
    
    
    
    'k2 = GraphicsWindow.LastKey
      If (k = "D") Then
      right2 = 1
    ElseIf (k = "A") then
      left2 = 1
      Elseif (k = "W") then
      up2 = 1
    Elseif (k = "S") then
      down2 = 1
    EndIf
    
    EndSub
      
      
      sub car1move
        
            If   (up = 1) And (right = 1) Then
         x = x + 1
          y = y - 1
        a= 135
    
         LDPhysics.SetPosition(car1, x, y, a)
    
       
     ElseIf ( left = 1) and (up = 1) then
    x = x - 1
       y = y - 1
        a= 45
    
    LDPhysics.SetPosition(car1, x, y, a)
    
    elseif  (right = 1) and ( down = 1) then
      y = y + 1
    x = x + 1
       a= 225
    
    LDPhysics.SetPosition(car1, x, y, a)
    
    elseif ( down = 1) and (left = 1) then
      y = y + 1
    x = x - 1
       a= -45
    
    LDPhysics.SetPosition(car1, x, y, a) 
    Else
        
        
        
        
        If  (right = 1) Then
        x = x + 1
        a= 180
    
         LDPhysics.SetPosition(car1, x, y, a)
    
    ElseIf ( left = 1) then
          x = x - 1
        a= 0
    
     LDPhysics.SetPosition(car1, x, y, a)
      elseif ( up = 1) then
           y = y - 1
        a= 90
    
     LDPhysics.SetPosition(car1, x, y, a)
    elseif ( down = 1) then
           y = y + 1
           a= -90
    
     LDPhysics.SetPosition(car1, x, y, a) 
    EndIf
    
    
    
    
    
    
    EndIf
    
    
      
    EndSub
    
    
    sub car2move
      
           If  (right2 = 1) And (up2 = 1) Then
    
    y2 = y2 - 1
    x2 = x2 + 1
        a2= 135
    
         LDPhysics.SetPosition(car2, x2, y2, a2)
    
       
     ElseIf ( left2 = 1) and (up2 =  1) then
    
       y2 = y2 - 1
    x2 = x2 - 1
    a2= 45
    
    LDPhysics.SetPosition(car2, x2, y2, a2)
    
    elseif  (right2 = 1) and ( down2 = 1) then
      y2 = y2 + 1
    x2 = x2 + 1
       a2= 225
    
    LDPhysics.SetPosition(car2, x2, y2, a2)
    
    elseif (left2 = 1) and ( down2 = 1) then
      y2 = y2 + 1
    x2 = x2 - 1
       a2= -45
    
     LDPhysics.SetPosition(car2, x2, y2, a2) 
    
    Else
    
    
    
    If  (right2 = 1) Then
        x2 = x2 + 1
        a2= 180
        LDPhysics.SetPosition(car2, x2, y2, a2)
         
    ElseIf ( left2 = 1) then
          x2 = x2 - 1
        a2= 0
        
     LDPhysics.SetPosition(car2, x2, y2, a2)
      elseif ( up2 = 1) then
           y2 = y2 - 1
        a2= 90
    
     LDPhysics.SetPosition(car2, x2, y2, a2)
    elseif ( down2 = 1) then
           y2 = y2 + 1
           a2 = -90
     
     LDPhysics.SetPosition(car2, x2, y2, a2) 
     EndIf
     
     
    
    
    
    
    EndIf
    
    EndSub

    Friday, December 21, 2012 2:55 AM

Answers

  • This is a limitation of common computer keyboard hardware!

    Most keyboards are not capable to reliably detect so many keys pressed at once.

    Everything more than 3 is already too much!!!

    See this Wikipedia article -> Rollover_key

    Just a wild guess -> maybe with 2 separate keyboards being used, 1 for each player, the key presses from one player won't jam and/or block the key presses of the other player!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Friday, December 21, 2012 3:13 AM
    Answerer
  • ... why sometimes although more than three keys are pressed, the program still works properly. While only under certain conditions when four keys are pressed the keyboard locks up?

    The very same Wikipedia's article has this post:

    Most modern action-oriented computer games and console emulators use the control, alt, and shift keys by default, since they are always designed to be pressed in combination with other keys, and thus usually circumvent key blocking.

    Thus, there are indeed some keyboard keys which are laid out in a way to be more immune to multi-key pressing errors.

    You have to find a 2-player key matrix combination which are less prone to jam each other.

    Or like I've already said, use 2 keyboards, or 1 keyboard + 1 joypad, or 2 joypads!  :D

    Or even, make your game w/ network capabilities!!!   @_@


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Friday, December 21, 2012 11:33 AM
    • Marked as answer by Behnam Azizi Saturday, December 22, 2012 3:28 AM
    Friday, December 21, 2012 11:25 AM
    Answerer

All replies

  • This is a limitation of common computer keyboard hardware!

    Most keyboards are not capable to reliably detect so many keys pressed at once.

    Everything more than 3 is already too much!!!

    See this Wikipedia article -> Rollover_key

    Just a wild guess -> maybe with 2 separate keyboards being used, 1 for each player, the key presses from one player won't jam and/or block the key presses of the other player!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Friday, December 21, 2012 3:13 AM
    Answerer
  • It can be the problem. But the strange thing is that only under certain conditions (as mentioned in my post) this happend. If it was a so-called rollover key problem then why sometimes although more than three keys are pressed, the program still works properly. While only under certain conditions when four keys are pressed the keyboard locks up?

    Thanks,

    Friday, December 21, 2012 4:49 AM
  • ... why sometimes although more than three keys are pressed, the program still works properly. While only under certain conditions when four keys are pressed the keyboard locks up?

    The very same Wikipedia's article has this post:

    Most modern action-oriented computer games and console emulators use the control, alt, and shift keys by default, since they are always designed to be pressed in combination with other keys, and thus usually circumvent key blocking.

    Thus, there are indeed some keyboard keys which are laid out in a way to be more immune to multi-key pressing errors.

    You have to find a 2-player key matrix combination which are less prone to jam each other.

    Or like I've already said, use 2 keyboards, or 1 keyboard + 1 joypad, or 2 joypads!  :D

    Or even, make your game w/ network capabilities!!!   @_@


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Friday, December 21, 2012 11:33 AM
    • Marked as answer by Behnam Azizi Saturday, December 22, 2012 3:28 AM
    Friday, December 21, 2012 11:25 AM
    Answerer
  • Wooow, this is actually a secret in the keyboard I never knew. It's very interesting that even among the alphabet keys, some have the priority.
    Saturday, December 22, 2012 3:30 AM