0

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 Answer 1

1

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

1
  • The Stdin was the issue there ! Thanks a lot
    – Otor
    Apr 7, 2022 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.