英文:
Disable Quick Edit in Golang
问题
我想在Golang中禁用快速编辑模式,以避免我的终端被用户点击阻塞。
为此,我使用了官方库golang.org/x/sys/windows。
我想要禁用QUICK_EDIT_MODE并启用EXTENDED_FLAGS,就像官方的微软文档中描述的那样。当我使用下面的代码片段时,我得到了一个设置错误的错误信息The parameter is incorrect.
无论我在CMD还是Powershell中运行它,都会得到相同的错误。我真的不知道缺少了什么。我使用的是Windows 10 v20H2 19042.1586。
代码片段:
import (
"os"
"golang.org/x/sys/windows"
)
...
func SetupConsole() {
winConsole := windows.Handle(os.Stdout.Fd())
var mode uint32
err := windows.GetConsoleMode(winConsole, &mode)
if err != nil {
log.Println(err)
}
log.Printf("%d", mode) // 3
// 禁用此模式
mode &^= windows.ENABLE_QUICK_EDIT_MODE
// 启用此模式
mode |= windows.ENABLE_EXTENDED_FLAGS
log.Printf("%d", mode) // 131
err = windows.SetConsoleMode(winConsole, mode)
if err != nil {
log.Println(err) // 这里失败:参数不正确。
}
}
编辑:请参见下面的答案
func SetupConsole() {
winConsole := windows.Handle(os.Stdin.Fd()) // 这里有错误
var mode uint32
err := windows.GetConsoleMode(winConsole, &mode)
if err != nil {
log.Println(err)
}
log.Printf("%d", mode)
// 禁用此模式
mode &^= windows.ENABLE_QUICK_EDIT_MODE
// 启用此模式
mode |= windows.ENABLE_EXTENDED_FLAGS
log.Printf("%d", mode)
err = windows.SetConsoleMode(winConsole, mode)
if err != nil {
log.Println(err)
}
}
英文:
I want to disable quick edit mode in Golang to avoid my terminal getting block by a user click.
For that I use the official library golang.org/x/sys/windows.
I want to disable QUICK_EDIT_MODE & enable EXTENDED_FLAGS as describe by the official microsoft doc. When I use the code snippet below, I'm getting an error at set The parameter is incorrect.
I'm running it in CMD or Powershell I get the same error there. And I don't really know what is missing there. I'm on Windows 10 v20H2 19042.1586 if that matters.
Code Snippet
import (
"os"
"golang.org/x/sys/windows"
)
...
func SetupConsole() {
winConsole := windows.Handle(os.Stdout.Fd())
var mode uint32
err := windows.GetConsoleMode(winConsole, &mode)
if err != nil {
log.Println(err)
}
log.Printf("%d", mode) <--- 3
// Disable this mode
mode &^= windows.ENABLE_QUICK_EDIT_MODE
// Enable this mode
mode |= windows.ENABLE_EXTENDED_FLAGS
log.Printf("%d", mode) <--- 131
err = windows.SetConsoleMode(winConsole, mode)
if err != nil {
log.Println(err) <--- This fails: The parameter is incorrect.
}
}
EDIT: See Answer below
func SetupConsole() {
winConsole := windows.Handle(os.Stdin.Fd()) <--- Was wrong here
var mode uint32
err := windows.GetConsoleMode(winConsole, &mode)
if err != nil {
log.Println(err)
}
log.Printf("%d", mode)
// Disable this mode
mode &^= windows.ENABLE_QUICK_EDIT_MODE
// Enable this mode
mode |= windows.ENABLE_EXTENDED_FLAGS
log.Printf("%d", mode)
err = windows.SetConsoleMode(winConsole, mode)
if err != nil {
log.Println(err)
}
}
答案1
得分: 1
我看到两个问题。
首先,你应该使用Stdin而不是Stdout。你也可以直接将windows.Stdin传递给Get/SetConsole mode,跳过Fd()调用和类型转换。
其次,要禁用它,你只需要切换windows.ENABLE_QUICK_EDIT_MODE标志。
使用Stdout和ENABLE_EXTENDED_FLAGS的组合导致了从SetConsoleMode返回的错误。
英文:
I see two issues.
First you should be using Stdin instead of Stdout. You can also just pass in windows.Stdin into the Get/SetConsole mode and skip the Fd() call and casting.
Second, to disable it you just need to toggle the windows.ENABLE_QUICK_EDIT_MODE flag.
The combination of using Stdout & ENABLE_EXTENDED_FLAGS together is what resulted in the error returing from SetConsoleMode
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论