drj14664 2019-07-20 23:53
浏览 445

GetKeyBoardState()始终显示某些键被按下,即使在一段时间不按键盘之后也没有

I am currently learning Go and trying to make a game which allows players to press several buttons at the same time. Right now, my code works for a while, but sometimes when I randomly press, the GetKeyboardState() always records one or more keys as pressed. The weird key only can be released when I press the key again. Then everything goes correctly again. For example, I randomly press Up, Down, Left, Right, and right shift bottom on the keyboard to control the character in the game. Suddenly, the character always tries to go right and if I press Up or Down arrow, the character will just go to the top right or the bottom right. Only if I press the right arrow button again, the character can stop go right.

I thought maybe the game goes too fast so I limit the fps under 90 but the situation still exists.

Also, I tried to print out the keyboard status and it always shows the weird bottom was pressed when the situation happened even if I did not touch my keyboard.

func GetInput(keyState []uint8, prevKeyState []uint8) []Input {
   input := make([]Input, 0)
   if keyState[sdl.SCANCODE_UP] != 0 {
       input = append(input, Input{Up})
   }
       ...
   // Handle Down, Right, Left
   if keyState[sdl.SCANCODE_RSHIFT] == 0 && prevKeyState[sdl.SCANCODE_RSHIFT] != 0 {
       input = append(input, Input{RShift})
   }
   if keyState[sdl.SCANCODE_B] == 0 && prevKeyState[sdl.SCANCODE_B] != 0 
       {
       input = append(input, Input{Debug})
   }

   // something like prevKeyState = keystate
   return input
}

How I use it

keyState := sdl.GetKeyboardState()
prevKeyState := make([]uint8, len(keyState))
for {
    ...
    input := ui.GetInput(keyState, prevKeyState)
    HandleInput(input, board)
    ...
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 C#调用python代码(python带有库)
    • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
    • ¥15 活动选择题。最多可以参加几个项目?
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题
    • ¥20 云服务Linux系统TCP-MSS值修改?
    • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
    • ¥20 怎么在stm32门禁成品上增加查询记录功能
    • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面