禁用Golang中的快速编辑功能

huangapple go评论130阅读模式
英文:

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。

代码片段:

  1. import (
  2. "os"
  3. "golang.org/x/sys/windows"
  4. )
  5. ...
  6. func SetupConsole() {
  7. winConsole := windows.Handle(os.Stdout.Fd())
  8. var mode uint32
  9. err := windows.GetConsoleMode(winConsole, &mode)
  10. if err != nil {
  11. log.Println(err)
  12. }
  13. log.Printf("%d", mode) // 3
  14. // 禁用此模式
  15. mode &^= windows.ENABLE_QUICK_EDIT_MODE
  16. // 启用此模式
  17. mode |= windows.ENABLE_EXTENDED_FLAGS
  18. log.Printf("%d", mode) // 131
  19. err = windows.SetConsoleMode(winConsole, mode)
  20. if err != nil {
  21. log.Println(err) // 这里失败:参数不正确。
  22. }
  23. }

编辑:请参见下面的答案

  1. func SetupConsole() {
  2. winConsole := windows.Handle(os.Stdin.Fd()) // 这里有错误
  3. var mode uint32
  4. err := windows.GetConsoleMode(winConsole, &mode)
  5. if err != nil {
  6. log.Println(err)
  7. }
  8. log.Printf("%d", mode)
  9. // 禁用此模式
  10. mode &^= windows.ENABLE_QUICK_EDIT_MODE
  11. // 启用此模式
  12. mode |= windows.ENABLE_EXTENDED_FLAGS
  13. log.Printf("%d", mode)
  14. err = windows.SetConsoleMode(winConsole, mode)
  15. if err != nil {
  16. log.Println(err)
  17. }
  18. }
英文:

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

  1. import (
  2. "os"
  3. "golang.org/x/sys/windows"
  4. )
  5. ...
  6. func SetupConsole() {
  7. winConsole := windows.Handle(os.Stdout.Fd())
  8. var mode uint32
  9. err := windows.GetConsoleMode(winConsole, &mode)
  10. if err != nil {
  11. log.Println(err)
  12. }
  13. log.Printf("%d", mode) <--- 3
  14. // Disable this mode
  15. mode &^= windows.ENABLE_QUICK_EDIT_MODE
  16. // Enable this mode
  17. mode |= windows.ENABLE_EXTENDED_FLAGS
  18. log.Printf("%d", mode) <--- 131
  19. err = windows.SetConsoleMode(winConsole, mode)
  20. if err != nil {
  21. log.Println(err) <--- This fails: The parameter is incorrect.
  22. }
  23. }

EDIT: See Answer below

  1. func SetupConsole() {
  2. winConsole := windows.Handle(os.Stdin.Fd()) <--- Was wrong here
  3. var mode uint32
  4. err := windows.GetConsoleMode(winConsole, &mode)
  5. if err != nil {
  6. log.Println(err)
  7. }
  8. log.Printf("%d", mode)
  9. // Disable this mode
  10. mode &^= windows.ENABLE_QUICK_EDIT_MODE
  11. // Enable this mode
  12. mode |= windows.ENABLE_EXTENDED_FLAGS
  13. log.Printf("%d", mode)
  14. err = windows.SetConsoleMode(winConsole, mode)
  15. if err != nil {
  16. log.Println(err)
  17. }
  18. }

答案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

huangapple
  • 本文由 发表于 2022年3月31日 17:45:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/71690354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定