dougu8742 2014-11-29 02:16
浏览 167
已采纳

如何对Go中的按键事件做出反应?

I have a REPL app in Go that should react to keyboard press events (distinct action for each key pressed key) but ReadString expects for return key to be pressed before reading os.Stdin:

import (
    "bufio"
    "os"
)

for {
    reader := bufio.NewReader(os.Stdin)
    key, _ := reader.ReadString('
')
    deferKey(key)
}

How can I react to key press events in Go?

  • 写回答

1条回答 默认 最新

  • douzhuo2722 2014-12-03 19:38
    关注

    Game engines commonly implement this kind of functionality. They are usually also pretty much platform agnostic (usually at least Windows, Linux, Mac OS X). Try for instance Azul3D's keyboard library.

    The logic is off the top of my head something like

    watcher := keyboard.NewWatcher()
    // Query for the map containing information about all keys
    status := watcher.States()
    left := status[keyboard.ArrowLeft]
    if left == keyboard.Down {
        // The arrow to left is being held down
        // Do something!
    }
    

    Getting a list of what keys are currently being pressed down is a matter of iterating the map through and listing the keys where value was Down.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA