使用 Ruby 在终端中捕获鼠标事件

问题描述 投票:0回答:1

这是我一直在尝试的一些代码:

print "\e[?1003h"

begin
  loop do
    begin
      input = ARGF.read_nonblock(1024)
      puts "input: #{input.inspect}"
    rescue IO::EAGAINWaitReadable
    end
  end
ensure
  print "\e[?1003l"
end

当我运行此代码时,我可以看到类似这样的内容被打印到终端,但没有一个表明它被发送到

ARGF
。我也尝试过
STDIN
。我的
read_nonblock
调用总是引发
IO::EAGAINWaitReadable
,这似乎是预期的行为(请参阅 spec)。

^[[MCn@^[[MCn?^[[MCn>^[[MCm>^[[MCm=^[[MCk;^[[MCk:^[[MCj:^[[MCi:^[[MCh:^[[MCg:^[[MCe:^[[MCd:^[[MCc:^[[MCb;^[[MCb<^[[MCc<^[[MCd<^[[MCe<^[[MCf<^[[MCg<^[[MCg;^[[MCf;^[[MCf:^[[MCe:^[[MCd:^[[MCd;^[[MCd<^[[MCe<^[[MCf<^[[MCg<^[[MCg;^[[MCg<^[[MCg;^[[MCf;^[[MCf:^[[MCe:^[[MCd:^[[MCd;^[[MCe;^[[MCe<^[[MCe;^[[MCf;^[[MCe;^[[MCe:^[[MCe;^[[MCf;^[[MCe;^[[MCd;^[[MCd<^[[MCe<^[[MCe=^[[MCf=^[[MCf<^[[MCe<^[[MCe;^[[MCd;^[[MCc;^[[MCc<^[[MCd<^[[MCe<^[[MCe;^[[MCe<^[[MCf<^[[MCf;^[[MCe;^[[MCd;^[[MCd<^[[MCe<^[[MCe=^[[MCf=^[[MCg=^[[MCh=^[[MCi>^[[MCj>^[[MCk>^[[MCl>^[[MCn?^[[MCpA^[[MCqB^[[MCrC^[[MCrD^[[MCsD^[[MCsE^[[MCtF^[[MCsG^[[MCrG^[[MCqG^[[MCpG^[[MCpH^[[MCmH^[[MCmG^[[MClG^[[MCgI^[[MCfI^[[MCeI^[[MC]I^[[MC\I^[[MC[I
ruby bash command-line-interface mouse ansi-escape
1个回答
0
投票

如果你想自己处理,你必须在

raw
模式下捕获字符。

这应该有效:(我试图保留你的代码结构)

require 'io/console'

at_exit { print "\e[?1003l" }

print "\e[?1003h"

STDIN.raw(intr: true) do
  loop do
    begin
      input = STDIN.read_nonblock(1024)
      p input: input
    rescue IO::EAGAINWaitReadable
      IO.select([STDIN])
      retry
    end
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.