【问题标题】:How can I read the arrow keys in a Ruby curses application?如何读取 Ruby 诅咒应用程序中的箭头键?
【发布时间】:2017-07-11 13:25:29
【问题描述】:

我有一个 Ruby curses 应用程序,我想在其中捕获箭头键和功能键。问题是使用STDIN.getch 时某些击键会生成多个值。当我输入像a-z 这样的“常规”键时,我会返回一个值。当我键入 [F] 键或箭头键时,我会返回三个值。

是否有专门用于处理键盘输入的 gem 或更好的方法来完成读取击键?

#!/usr/bin/ruby

require 'curses'
require 'io/console'

Curses.noecho

Curses.init_screen
main_window = Curses::Window.new(24, 40, 1, 0)

num_keys = 0
loop do
   ch = STDIN.getch
   num_keys = num_keys + 1
   main_window.addstr(' key:' + ch.inspect + ' count:' + num_keys.to_s)
   main_window.refresh

   break if ch == 'q'
end

Curses.close_screen

【问题讨论】:

    标签: ruby keyboard ncurses


    【解决方案1】:

    尝试在实例化后立即在窗口上启用keypad

    main_window = Curses::Window.new(24, 40, 1, 0)
    main_window.keypad = true
    

    然后你可以在窗口上使用getch 方法而不是使用STDIN.getch,因此请尝试更改

    ch = STDIN.getch
    

    ch = main_window.getch
    

    现在当我运行你的程序时,我得到了

    键:259 计数:1

    当我点击向上箭头而不是

    key:"\e" count 1 key:"[" count:2 key:"A" count:3

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 2018-12-18
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      相关资源
      最近更新 更多