This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeybind_windows.go
113 lines (96 loc) · 2.22 KB
/
keybind_windows.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package keybind
import (
"os"
"syscall"
"unsafe"
"github.com/mattn/go-isatty"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetStdHandle = kernel32.NewProc("GetStdHandle")
procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
procReadConsoleInput = kernel32.NewProc("ReadConsoleInputW")
)
const (
enableLineInput = 2
enableEchoInput = 4
enableProcessedInput = 1
enableWindowInput = 8
enableMouseInput = 16
enableInsertMode = 32
enableQuickEditMode = 64
enableExtendedFlags = 128
enableAutoPosition = 256
enableProcessedOutput = 1
enableWrapAtEolOutput = 2
keyEvent = 0x1
mouseEvent = 0x2
windowBufferSizeEvent = 0x4
)
type Term struct {
in uintptr
orgTerm uint32
}
type wchar uint16
type short int16
type dword uint32
type word uint16
type inputRecord struct {
eventType word
padding [2]byte
event [16]byte
}
type keyEventRecord struct {
keyDown int32
repeatCount word
virtualKeyCode word
virtualScanCode word
unicodeChar wchar
controlKeyState dword
}
func Open() *Term {
var in uintptr
if isatty.IsTerminal(os.Stdin.Fd()) {
in = getStdHandle(syscall.STD_INPUT_HANDLE)
} else {
conin, _ := os.Open("CONIN$")
in = conin.Fd()
}
var orgTerm uint32
procGetConsoleMode.Call(in, uintptr(unsafe.Pointer(&orgTerm)))
rawTerm := orgTerm
rawTerm &^= (enableEchoInput | enableLineInput)
procSetConsoleMode.Call(in, uintptr(rawTerm))
return &Term{
in: in,
orgTerm: orgTerm,
}
}
func (t *Term) ReadRune() (rune, error) {
for {
var ir inputRecord
var w uint32
ok, _, err := procReadConsoleInput.Call(t.in, uintptr(unsafe.Pointer(&ir)), 1, uintptr(unsafe.Pointer(&w)))
if ok == 0 && err != nil {
return NUL, err
}
switch ir.eventType {
case keyEvent:
kr := (*keyEventRecord)(unsafe.Pointer(&ir.event))
if kr.keyDown != 0 {
ch := rune(kr.unicodeChar)
if ch > 0 {
return ch, nil
}
}
}
}
}
func (t *Term) Close() {
procSetConsoleMode.Call(t.in, uintptr(t.orgTerm))
}
func getStdHandle(stdhandle int32) uintptr {
handle, _, _ := procGetStdHandle.Call(uintptr(stdhandle))
return handle
}