提交 d9cda937 编写于 作者: N nsf

Disable mouse events by default. Allow ORing InputMode as flags.

Add InputMouse flag, which controls the mouse button click events.
上级 682ffcd2
......@@ -493,13 +493,19 @@ func draw_keyboard() {
printf_tb(21, 2, termbox.ColorMagenta, termbox.ColorBlack, "(press CTRL+X and then CTRL+Q to exit)")
printf_tb(15, 3, termbox.ColorMagenta, termbox.ColorBlack, "(press CTRL+X and then CTRL+C to change input mode)")
var inputmodemap = []string{
"",
"termbox.InputEsc",
"termbox.InputAlt",
inputmode := termbox.SetInputMode(termbox.InputCurrent)
inputmode_str := ""
switch {
case inputmode & termbox.InputEsc != 0:
inputmode_str = "termbox.InputEsc"
case inputmode & termbox.InputAlt != 0:
inputmode_str = "termbox.InputAlt"
}
printf_tb(3, 18, termbox.ColorWhite, termbox.ColorBlack, "Input mode: %s",
inputmodemap[termbox.SetInputMode(termbox.InputCurrent)])
if inputmode & termbox.InputMouse != 0 {
inputmode_str += " | termbox.InputMouse"
}
printf_tb(3, 18, termbox.ColorWhite, termbox.ColorBlack, "Input mode: %s", inputmode_str)
}
var fcmap = []string{
......@@ -581,17 +587,17 @@ func pretty_print_press(ev *termbox.Event) {
printf_tb(8, 21, termbox.ColorCyan, termbox.ColorBlack, "octal: 0%o", ev.Key)
printf_tb(8, 22, termbox.ColorRed, termbox.ColorBlack, "string: %s", funckeymap(ev.Key))
printf_tb(43, 19, termbox.ColorWhite, termbox.ColorBlack, "Char: ")
printf_tb(49, 19, termbox.ColorYellow, termbox.ColorBlack, "decimal: %d", ev.Ch)
printf_tb(49, 20, termbox.ColorGreen, termbox.ColorBlack, "hex: 0x%X", ev.Ch)
printf_tb(49, 21, termbox.ColorCyan, termbox.ColorBlack, "octal: 0%o", ev.Ch)
printf_tb(49, 22, termbox.ColorRed, termbox.ColorBlack, "string: %s", string(ev.Ch))
printf_tb(54, 19, termbox.ColorWhite, termbox.ColorBlack, "Char: ")
printf_tb(60, 19, termbox.ColorYellow, termbox.ColorBlack, "decimal: %d", ev.Ch)
printf_tb(60, 20, termbox.ColorGreen, termbox.ColorBlack, "hex: 0x%X", ev.Ch)
printf_tb(60, 21, termbox.ColorCyan, termbox.ColorBlack, "octal: 0%o", ev.Ch)
printf_tb(60, 22, termbox.ColorRed, termbox.ColorBlack, "string: %s", string(ev.Ch))
modifier := "none"
if ev.Mod != 0 {
modifier = "termbox.ModAlt"
}
printf_tb(43, 18, termbox.ColorWhite, termbox.ColorBlack, "Modifier: %s", modifier)
printf_tb(54, 18, termbox.ColorWhite, termbox.ColorBlack, "Modifier: %s", modifier)
}
func pretty_print_resize(ev *termbox.Event) {
......@@ -649,11 +655,12 @@ func main() {
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
draw_keyboard()
termbox.Flush()
inputmode := 0
ctrlxpressed := false
loop:
for {
......@@ -664,12 +671,16 @@ loop:
}
if ev.Key == termbox.KeyCtrlC && ctrlxpressed {
chmap := []termbox.InputMode{
0,
termbox.InputEsc | termbox.InputMouse,
termbox.InputAlt | termbox.InputMouse,
termbox.InputAlt,
termbox.InputEsc,
}
termbox.SetInputMode(
chmap[termbox.SetInputMode(termbox.InputCurrent)])
inputmode++
if inputmode >= len(chmap) {
inputmode = 0
}
termbox.SetInputMode(chmap[inputmode])
}
if ev.Key == termbox.KeyCtrlX {
ctrlxpressed = true
......
......@@ -71,7 +71,6 @@ func Init() error {
out.WriteString(funcs[t_enter_ca])
out.WriteString(funcs[t_enter_keypad])
out.WriteString(funcs[t_enter_mouse])
out.WriteString(funcs[t_hide_cursor])
out.WriteString(funcs[t_clear_screen])
......@@ -277,16 +276,26 @@ func Clear(fg, bg Attribute) error {
// Sets termbox input mode. Termbox has two input modes:
//
// 1. Esc input mode. When ESC sequence is in the buffer and it doesn't match
// any known sequence. ESC means KeyEsc.
// any known sequence. ESC means KeyEsc. This is the default input mode.
//
// 2. Alt input mode. When ESC sequence is in the buffer and it doesn't match
// any known sequence. ESC enables ModAlt modifier for the next keyboard event.
//
// Both input modes can be OR'ed with Mouse mode. Setting Mouse mode bit up will
// enable mouse button click events.
//
// If 'mode' is InputCurrent, returns the current input mode. See also Input*
// constants.
func SetInputMode(mode InputMode) InputMode {
if mode != InputCurrent {
input_mode = mode
if mode == InputCurrent {
return input_mode
}
if mode & InputMouse != 0 {
out.WriteString(funcs[t_enter_mouse])
} else {
out.WriteString(funcs[t_exit_mouse])
}
input_mode = mode
return input_mode
}
......@@ -142,9 +142,10 @@ const (
// Input mode. See SetInputMode function.
const (
InputCurrent InputMode = iota
InputEsc
InputEsc InputMode = 1 << iota
InputAlt
InputMouse
InputCurrent InputMode = 0
)
// Event type. See Event.Type field.
......
......@@ -30,7 +30,7 @@ func Init() error {
return err
}
err = set_console_mode(in, enable_window_input|enable_mouse_input)
err = set_console_mode(in, enable_window_input)
if err != nil {
return err
}
......@@ -155,16 +155,32 @@ func Clear(fg, bg Attribute) error {
// Sets termbox input mode. Termbox has two input modes:
//
// 1. Esc input mode. When ESC sequence is in the buffer and it doesn't match
// any known sequence. ESC means KeyEsc.
// any known sequence. ESC means KeyEsc. This is the default input mode.
//
// 2. Alt input mode. When ESC sequence is in the buffer and it doesn't match
// any known sequence. ESC enables ModAlt modifier for the next keyboard event.
//
// Both input modes can be OR'ed with Mouse mode. Setting Mouse mode bit up will
// enable mouse button click events.
//
// If 'mode' is InputCurrent, returns the current input mode. See also Input*
// constants.
func SetInputMode(mode InputMode) InputMode {
if mode != InputCurrent {
input_mode = mode
if mode == InputCurrent {
return input_mode
}
if mode & InputMouse != 0 {
err = set_console_mode(in, enable_window_input|enable_mouse_input)
if err != nil {
panic(err)
}
} else {
err = set_console_mode(in, enable_window_input)
if err != nil {
panic(err)
}
}
input_mode = mode
return input_mode
}
......@@ -263,8 +263,8 @@ func extract_event(event *Event) bool {
}
// it's not escape sequence, then it's Alt or Esc, check input_mode
switch input_mode {
case InputEsc:
switch {
case input_mode & InputEsc != 0:
// if we're in escape mode, fill Esc event, pop buffer, return success
event.Ch = 0
event.Key = KeyEsc
......@@ -272,7 +272,7 @@ func extract_event(event *Event) bool {
copy(inbuf, inbuf[1:])
inbuf = inbuf[:len(inbuf)-1]
return true
case InputAlt:
case input_mode & InputAlt != 0:
// if we're in alt mode, set Alt modifier to event and redo parsing
event.Mod = ModAlt
copy(inbuf, inbuf[1:])
......
......@@ -521,7 +521,7 @@ func key_event_record_to_event(r *key_event_record) (Event, bool) {
}
e := Event{Type: EventKey}
if input_mode == InputAlt {
if input_mode & InputAlt != 0 {
if alt_mode_esc {
e.Mod = ModAlt
alt_mode_esc = false
......@@ -599,10 +599,10 @@ func key_event_record_to_event(r *key_event_record) (Event, bool) {
case vk_enter:
e.Key = KeyEnter
case vk_esc:
switch input_mode {
case InputEsc:
switch {
case input_mode & InputEsc != 0:
e.Key = KeyEsc
case InputAlt:
case input_mode & InputAlt != 0:
alt_mode_esc = true
return Event{}, false
}
......@@ -624,7 +624,7 @@ func key_event_record_to_event(r *key_event_record) (Event, bool) {
if ctrlpressed {
if Key(r.unicode_char) >= KeyCtrlA && Key(r.unicode_char) <= KeyCtrlRsqBracket {
e.Key = Key(r.unicode_char)
if input_mode == InputAlt && e.Key == KeyEsc {
if input_mode & InputAlt != 0 && e.Key == KeyEsc {
alt_mode_esc = true
return Event{}, false
}
......@@ -636,7 +636,7 @@ func key_event_record_to_event(r *key_event_record) (Event, bool) {
e.Key = KeyCtrl2
return e, true
case 51:
if input_mode == InputAlt {
if input_mode & InputAlt != 0 {
alt_mode_esc = true
return Event{}, false
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册