Rubyを学びたいので、同時にCursesを試してみると思っていました。私は、ユーザーがシステムコマンドを実行するオプションを選択できるナビゲーションメニューを作成しています。Cursesを使用しているときにReturnキーまたはEnterキーを正しく検出する方法
私はナビゲーションメニューが作成された時点で、矢印キーを使用してオプションを選択することができます。
これでEnterキーを入力し、Enterキーを押したときにシステムコマンドを実行しようとしています。例:
position = 3 if position < 0
position = 0 if position > 3
draw_menu(menu, position)
if position == 0
draw_info menu, 'You selected option 0'
input = menu.getch
if input == ENTER
menu.clear
menu.refresh
puts (system 'ls')
end
Enterキーを押すと、システムコマンドが動作しない(の一種)で
input = menu.getch
if input == ENTER
が、私はオプションがある場合は、システムのコマンドも実行される問題を抱えています選択または強調表示されました。 Enterキーを押すだけで機能します。
私は
if input == 'k'
に変更する場合は、Enterキーを押したときにシステムコマンドのみが実行されます。強調表示または選択したときは実行されません。これは私がそれが働く方法です。
Enterキーを 'k'キーと同じように機能させるためのアイデアはありますか?
ここに私のコードです。
require 'curses'
include Curses
# Top Lie
SCREEN_WIDTH = 90
HEADER_HEIGHT = 4
HEADER_WIDTH = SCREEN_WIDTH
# Bottom Line
SCREEN_WIDTH2 = 90
HEADER_HEIGHT2 = 1
HEADER_WIDTH2 = SCREEN_WIDTH2
Curses.init_screen
Curses.curs_set(0) # Invisible cursor
Curses.start_color
Curses.noecho # echo or noecho to display user input
Curses.nonl
Curses.raw
Curses.stdscr.nodelay = 1
Curses.init_pair(1, Curses::COLOR_WHITE, Curses::COLOR_BLUE)
Curses.init_pair(2, Curses::COLOR_WHITE, Curses::COLOR_BLUE)
begin
# Top Line
header_window = Curses::Window.new(HEADER_HEIGHT, HEADER_WIDTH, 0, 0) # (height, width, top, left)
header_window.color_set(1)
header_window << "Curses example".center(HEADER_WIDTH)
header_window.refresh
# Bottom Line
header2_window = Curses::Window.new(HEADER_HEIGHT, HEADER_WIDTH, 23, 0)
header2_window.color_set(2)
header2_window << "Curses example".center(HEADER_WIDTH)
header2_window.refresh
# Building a static window
def draw_menu(menu, active_index=nil)
["This is option 0.", "This is option 1.", "This is option 2.", "This is option 3."].each_with_index do |element, index|
# "w" for word array
# It's a shortcut for arrays
menu.setpos(index + 1, 1)
menu.attrset(index == active_index ? A_STANDOUT : A_NORMAL)
menu.addstr("#{index} - %-10s" % element) # %-Xs makes sure array words line up evenly if you place index after element
# you can change 17 to another number
end
menu.setpos(5, 1)
end
def draw_info(menu, text)
menu.setpos(6, 1) # sets the position of move up and down
# for example, menu.setpos(1, 10) moves to another
# location
menu.attrset(A_NORMAL)
menu.addstr text
end
position = 0
menu = Window.new(20, 70, 2, 2) # (height, width, top, left)
menu.keypad = true # enable keypad which allows arrow keys
#menu.box('|', '-')
draw_menu(menu, position)
while ch = menu.getch
stdscr.keypad = true
case ch
when KEY_UP, 'w'
#draw_info menu, 'move up'
position -= 1
when KEY_DOWN, 's'
#draw_info menu, 'move down'
position += 1
when 'x'
exit
end
position = 3 if position < 0
position = 0 if position > 3
draw_menu(menu, position)
if position == 0
draw_info menu, 'You selected option 0'
input = menu.getch
if input == 'k' # I want this to be ENTER
menu.clear
menu.refresh
puts (system 'ls') # This does not work well. I need to fix it.
end
elsif position == 1
draw_info menu, 'You selected option 1'
elsif position == 2
draw_info menu, 'You selected option 2'
else position == 3
draw_info menu, 'You selected option 3'
end
end
rescue => ex
Curses.close_screen
end