Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

运行程序后方向键不可用 #29

Closed
shiluanzzz opened this issue Aug 17, 2022 · 9 comments
Closed

运行程序后方向键不可用 #29

shiluanzzz opened this issue Aug 17, 2022 · 9 comments
Labels
bug Something isn't working wait upstream

Comments

@shiluanzzz
Copy link

  • 在使用仓库提供的demo,执行前方向键可以正常使用,执行后方向键不可用,会被识别成[[A字符
    image

  • windows终端,winterm, cmd下都出现了这个问题。

  • 编译成可执行文件后在执行同样出现。

  • go version go1.18.1 windows/amd64

@fzdwx
Copy link
Owner

fzdwx commented Aug 17, 2022

这个切换的key是tab

@fzdwx
Copy link
Owner

fzdwx commented Aug 17, 2022

ok, 更新了0.8.3 14bad27 有help view了。

@shiluanzzz
Copy link
Author

这个切换key是tab了解,但是我运行这个程序后,已经结束了。在终端中就没法使用方向键了。按方向键会被识别成^[[A等字符。这个问题没有复现吗?

@fzdwx
Copy link
Owner

fzdwx commented Aug 17, 2022

cmd是吧,我试试

@fzdwx
Copy link
Owner

fzdwx commented Aug 17, 2022

ok,我也出现了,我看看

@fzdwx
Copy link
Owner

fzdwx commented Aug 17, 2022

@shiluanzzz 我好像找到原因了,跟这个方法有关https://github.com/muesli/cancelreader/blob/main/cancelreader_windows.go#L189

现在有一个简单的修复办法,你可以运行完demo之后在运行:

func main() {
	var originalMode uint32
	windows.SetConsoleMode(windows.Handle(os.Stdin.Fd()), originalMode)
}

我晚上想想怎么写。

@fzdwx
Copy link
Owner

fzdwx commented Aug 17, 2022

问题很诡异。 跟 window#GetConsoleMode 这个函数有关,当这个框架的程序运行时读取到的值是992, 但是默认的却是 503。就会导致这个情况出现。如果要改的话,只能强行把这个值改成 503

demo code:

package main

import (
	"fmt"
	"golang.org/x/sys/windows"
	"unicode/utf16"
)

var fileShareValidFlags uint32 = 0x00000007

func main() {
	var originalMode uint32

	handle, _ := windows.CreateFile(
		&(utf16.Encode([]rune("CONIN$\x00"))[0]), windows.GENERIC_READ|windows.GENERIC_WRITE,
		fileShareValidFlags, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_OVERLAPPED, 0)

	_ = windows.GetConsoleMode(handle, &originalMode)

    // 503
	fmt.Println(originalMode)

	// problematic mode
	//windows.SetConsoleMode(handle, 992)
}

link:

  1. https://docs.microsoft.com/zh-cn/windows/console/setconsolemode
  2. 程序里面读取到的值992: https://github.com/muesli/cancelreader/blob/main/cancelreader_windows.go#L192
  3. reset为903: https://github.com/muesli/cancelreader/blob/main/cancelreader_windows.go#L222

@fzdwx fzdwx moved this to Todo in infinite 迭代 Aug 17, 2022
@fzdwx fzdwx moved this from Todo to Ice in infinite 迭代 Aug 17, 2022
@fzdwx fzdwx closed this as completed in 458d6ae Aug 17, 2022
Repository owner moved this from Ice to Done in infinite 迭代 Aug 17, 2022
@fzdwx fzdwx reopened this Aug 17, 2022
Repository owner moved this from Done to In Progress in infinite 迭代 Aug 17, 2022
@fzdwx fzdwx moved this from In Progress to Ice in infinite 迭代 Aug 17, 2022
@fzdwx
Copy link
Owner

fzdwx commented Aug 17, 2022

@shiluanzzz 你拉下最新的代码,在试试

@shiluanzzz
Copy link
Author

good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wait upstream
Projects
Status: Ice
Development

No branches or pull requests

2 participants