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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题