Path: blob/main/vendor/github.com/chzyer/readline/rawreader_windows.go
2875 views
// +build windows12package readline34import "unsafe"56const (7VK_CANCEL = 0x038VK_BACK = 0x089VK_TAB = 0x0910VK_RETURN = 0x0D11VK_SHIFT = 0x1012VK_CONTROL = 0x1113VK_MENU = 0x1214VK_ESCAPE = 0x1B15VK_LEFT = 0x2516VK_UP = 0x2617VK_RIGHT = 0x2718VK_DOWN = 0x2819VK_DELETE = 0x2E20VK_LSHIFT = 0xA021VK_RSHIFT = 0xA122VK_LCONTROL = 0xA223VK_RCONTROL = 0xA324)2526// RawReader translate input record to ANSI escape sequence.27// To provides same behavior as unix terminal.28type RawReader struct {29ctrlKey bool30altKey bool31}3233func NewRawReader() *RawReader {34r := new(RawReader)35return r36}3738// only process one action in one read39func (r *RawReader) Read(buf []byte) (int, error) {40ir := new(_INPUT_RECORD)41var read int42var err error43next:44err = kernel.ReadConsoleInputW(stdin,45uintptr(unsafe.Pointer(ir)),461,47uintptr(unsafe.Pointer(&read)),48)49if err != nil {50return 0, err51}52if ir.EventType != EVENT_KEY {53goto next54}55ker := (*_KEY_EVENT_RECORD)(unsafe.Pointer(&ir.Event[0]))56if ker.bKeyDown == 0 { // keyup57if r.ctrlKey || r.altKey {58switch ker.wVirtualKeyCode {59case VK_RCONTROL, VK_LCONTROL:60r.ctrlKey = false61case VK_MENU: //alt62r.altKey = false63}64}65goto next66}6768if ker.unicodeChar == 0 {69var target rune70switch ker.wVirtualKeyCode {71case VK_RCONTROL, VK_LCONTROL:72r.ctrlKey = true73case VK_MENU: //alt74r.altKey = true75case VK_LEFT:76target = CharBackward77case VK_RIGHT:78target = CharForward79case VK_UP:80target = CharPrev81case VK_DOWN:82target = CharNext83}84if target != 0 {85return r.write(buf, target)86}87goto next88}89char := rune(ker.unicodeChar)90if r.ctrlKey {91switch char {92case 'A':93char = CharLineStart94case 'E':95char = CharLineEnd96case 'R':97char = CharBckSearch98case 'S':99char = CharFwdSearch100}101} else if r.altKey {102switch char {103case VK_BACK:104char = CharBackspace105}106return r.writeEsc(buf, char)107}108return r.write(buf, char)109}110111func (r *RawReader) writeEsc(b []byte, char rune) (int, error) {112b[0] = '\033'113n := copy(b[1:], []byte(string(char)))114return n + 1, nil115}116117func (r *RawReader) write(b []byte, char rune) (int, error) {118n := copy(b, []byte(string(char)))119return n, nil120}121122func (r *RawReader) Close() error {123return nil124}125126127