Skip to content

Instantly share code, notes, and snippets.

@spaceface777
Created September 26, 2020 13:23
Show Gist options
  • Save spaceface777/cf9df6a15ab91ed7d70b8cdfb2109a37 to your computer and use it in GitHub Desktop.
Save spaceface777/cf9df6a15ab91ed7d70b8cdfb2109a37 to your computer and use it in GitHub Desktop.
linux terminal mouse input in V
import os
os.system('stty -icanon')
os.system('stty -echo')
println('\x1b[?1003h\x1b[?1015h\x1b[?1006h')
for signal in 0 .. 64 {
os.signal(signal, fn() {
os.system('stty icanon')
os.system('stty echo')
println('\x1b[?1003l\x1b[?1015l\x1b[?1006l')
exit(0)
})
}
for {
mut buf := []byte{ len: 16 } // byteptr(malloc(16))
nr_chars := C.read(C.STDIN_FILENO, buf.data, 16)
if nr_chars < 3 { continue }
str := buf[3..nr_chars].bytestr()
split := str.split(';')
if split.len < 3 { continue }
x, y := split[1].int(), split[2].int()
if split[0] == '0' {
last := str[nr_chars - 4]
if last == `M` { println('($x, $y) LEFT MOUSE DOWN') }
if last == `m` { println('($x, $y) LEFT MOUSE UP') }
} else if split[0] == '2' {
last := str[nr_chars - 4]
if last == `M` { println('($x, $y) MIDDLE MOUSE DOWN') }
if last == `m` { println('($x, $y) MIDDLE MOUSE UP') }
} else if split[0] == '32' {
println('($x, $y) MOUSE DRAG')
} else if split[0] == '35' {
println('($x, $y) MOUSE MOVE')
} else if split[0] == '64' {
println('($x, $y) SCROLL UP')
} else if split[0] == '65' {
println('($x, $y) SCROLL DOWN')
} else {
println('OTHER | ${str:-16} | $str.len')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment