gpt4 book ai didi

ruby - 如何检查是否在 Ruby 和 Windows 控制台中按下了箭头键

转载 作者:可可西里 更新时间:2023-11-01 09:37:37 25 4
gpt4 key购买 nike

我正在使用 Ruby 2.1.5 和 Windows 控制台。我需要能够在不停止执行的情况下扫描按键。我需要能够专门检测箭头键。我已经尝试了很多关于其他问题的推荐方法,但是它们都不能在 Windows 控制台上工作或者不能检测到箭头键。

不显示方向键

require 'io/console'
loop do
p STDIN.getch
end

运行没有错误,但似乎没有从输入缓冲区读取,因为在使用 Ctrl-C 退出程序后,所有按下的键都显示在控制台上

require 'io/wait'

def char_if_pressed
begin

system("stty raw -echo") # turn raw input on
c = nil
if $stdin.ready?
c = $stdin.getc
end
c.chr if c
ensure
system "stty -raw echo" # turn raw input off
end
end

while true
c = char_if_pressed
puts "[#{c}]" if c
sleep 1
puts "tick"
end

此程序也可以正常运行,但需要按回车键才能继续运行。我需要非阻塞输入

# read a character without pressing enter and without printing to the screen
def read_char
begin
# save previous state of stty
old_state = "stty raw -g"
# disable echoing and enable raw (not having to press enter)
system "stty raw -echo"
c = STDIN.getc.chr
# gather next two characters of special keys
if(c=="\e")
puts "checking for non alpha"
extra_thread = Thread.new{
c = c + STDIN.getc.chr
c = c + STDIN.getc.chr
}
# wait just long enough for special keys to get swallowed
extra_thread.join(0.00001)
# kill thread so not-so-long special keys don't wait on getc
extra_thread.kill
end
rescue => ex
puts "#{ex.class}: #{ex.message}"
puts ex.backtrace
ensure
# restore previous state of stty
system "stty #{old_state}"
end
return c
end

# takes a single character command
def show_single_key
c = read_char
case c
when " "
puts "SPACE"
when "\t"
puts "TAB"
when "\r"
puts "RETURN"
when "\n"
puts "LINE FEED"
when "\e"
puts "ESCAPE"
when "\e[A"
puts "UP ARROW"
when "\e[B"
puts "DOWN ARROW"
when "\e[C"
puts "RIGHT ARROW"
when "\e[D"
puts "LEFT ARROW"
when "\177"
puts "BACKSPACE"
when "\004"
puts "DELETE"
when /^.$/
puts "SINGLE CHAR HIT: #{c.inspect}"
else
puts "SOMETHING ELSE: #{c.inspect}"
end
end
show_single_key while(true)

这似乎是上一个版本的变体,它对所有 ASCII 表示的键都能正常工作,但对箭头键不起作用,并且在回车时有问题。

require 'io/console'

# Reads keypresses from the user including 2 and 3 escape character sequences.
def read_char
STDIN.echo = false
STDIN.raw!

input = STDIN.getc.chr
puts input
if input == "\e" then
puts "getting additional characters"
input << STDIN.read_nonblock(3) rescue nil
input << STDIN.read_nonblock(2) rescue nil
end
ensure
#STDIN.echo = true
STDIN.cooked!

return input
end

# oringal case statement from:
# http://www.alecjacobson.com/weblog/?p=75
def show_single_key
c = read_char

case c
when " "
puts "SPACE"
when "\t"
puts "TAB"
when "\r"
puts "RETURN"
when "\n"
puts "LINE FEED"
when "\e"
puts "ESCAPE"
when "\e[A"
puts "UP ARROW"
when "\e[B"
puts "DOWN ARROW"
when "\e[C"
puts "RIGHT ARROW"
when "\e[D"
puts "LEFT ARROW"
when "\177"
puts "BACKSPACE"
when "\004"
puts "DELETE"
when "\e[3~"
puts "ALTERNATE DELETE"
when "\u0003"
puts "CONTROL-C"
exit 0
when /^.$/
puts "SINGLE CHAR HIT: #{c.inspect}"
else
puts "SOMETHING ELSE: #{c.inspect}"
end
end

show_single_key while(true)

我还尝试了几个具有 Win32API 调用的,但它们都无法正常工作,并且有关于被弃用的警告。我想要一个跨平台的解决方案,但在这一点上我会满足于它在 Windows 控制台上正常工作。我也不能为此使用任何 Ruby Gems。

最佳答案

我已经尝试解决这个问题一段时间了,这对我有用:

require 'io/console'
while true
user_input = $stdin.getch
case user_input
when 'q'
exit
when '['
print '' # I believe that by entering this block of code user_input drops the first char. What is left can be used to determine what key has been pressed.
else
case user_input
when 'A'
puts "up"
when 'B'
puts "down"
when 'C'
puts "right"
when 'D'
puts "left"
end
end
end

我使用的是 ruby​​ 2.4.1。希望能帮助到你。

关于ruby - 如何检查是否在 Ruby 和 Windows 控制台中按下了箭头键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29199520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com