summaryrefslogtreecommitdiff
path: root/sample/mouse.rb
blob: 92c2b963cc0135a8f203204ea068571a5f0b6b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby

require "curses"
include Curses

def show_message(*msgs)
  message = msgs.join
  height, width = 5, message.length + 6
  top, left = (lines - height) / 2, (cols - width) / 2
  win = Window.new(height, width, top, left)
  win.keypad = true
  win.attron(color_pair(COLOR_RED)) do
    win.box("|", "-", "+")
  end
  win.setpos(2, 3)
  win.addstr(message)
  win.refresh
  win.getch
  win.close
end

init_screen
start_color
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_WHITE)
init_pair(COLOR_RED, COLOR_RED, COLOR_WHITE)
crmode
noecho
stdscr.keypad(true)

begin
  mousemask(BUTTON1_CLICKED|BUTTON2_CLICKED|BUTTON3_CLICKED|BUTTON4_CLICKED)
  setpos((lines - 1) / 2, (cols - 5) / 2)
  attron(color_pair(COLOR_BLUE)|A_BOLD) do
    addstr("click")
  end
  refresh
  loop do
    c = getch
    case c
    when KEY_MOUSE
      m = getmouse
      if m
        show_message("getch = #{c.inspect}, ",
                     "mouse event = #{'0x%x' % m.bstate}, ",
                     "axis = (#{m.x},#{m.y},#{m.z})")
      end
      break
    end
  end
  refresh
ensure
  close_screen
end