FAQ
I'm looking for help with a Windows keyboard problem. I need to be able to
see individual keystrokes. I figured out how to do a kernel32
SetConsoleMode() to change from line mode to character mode. This works well,
except for the Esc key - which I need as part of the user interface.



Windows seems to eat the Esc key. When I use os.StdinRead() or fmt.Scanf()
nothing at all is returned. If I type Ctrl-[ however, I do get an escape
character.



Can anyone recommend a solution for this? I don't want to permanently remap
the keyscan codes in the registry because it would affect other programs. So
I'm looking for something I can do from my Go program.



Thanks!



John

     John Souvestre - New Orleans LA



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Brainman at Nov 21, 2014 at 10:18 pm
    I doubt anyone here knows the answer. You should try some Windows forum. Perhaps stackoverflow ...

    Alex

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Khomoutov at Nov 22, 2014 at 5:31 pm

    On Fri, 21 Nov 2014 12:46:44 -0600 "John Souvestre" wrote:

    I'm looking for help with a Windows keyboard problem. I need to be
    able to see individual keystrokes. I figured out how to do a kernel32
    SetConsoleMode() to change from line mode to character mode. This
    works well, except for the Esc key - which I need as part of the user
    interface.



    Windows seems to eat the Esc key. When I use os.StdinRead() or
    fmt.Scanf() nothing at all is returned. If I type Ctrl-[ however, I
    do get an escape character.
    I don't think that would work because this code is at the wrong layer:
    stdin (standard input) is a "cooked" stream of data which meant it has
    been "preprocessed" by the console implementation.

    Instead, you want to get hold on the "raw" state of the console.
    For this, there's no generic solution, and you'll have to resort to
    calling into Win32 API directly.

    See this [1] for more info. Basically, there two approaches are
    recommended:
    1) Plug your own handler into the message queue of the window
        hosting the console.
    2) Use a special (blocking) API call which is able to fetch most of
        keypresses (except for Ctrl, Pause etc).

    1. http://stackoverflow.com/q/2067893/720999

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • John Souvestre at Nov 22, 2014 at 5:38 pm
    I was headed down that path, but came up with an easier way.

         // #include <conio.h>
         import "C"
         ...
         key := rune(C._getch())

    This seems to do the job for me.

    Thanks,

    John

         John Souvestre - New Orleans LA

    -----Original Message-----
    From: golang-nuts@googlegroups.com On Behalf Of Konstantin Khomoutov
    Sent: 2014 November 22, Sat 11:32
    To: John Souvestre
    Cc: golang-nuts@googlegroups.com
    Subject: Re: [go-nuts] Escape Key

    On Fri, 21 Nov 2014 12:46:44 -0600
    "John Souvestre" wrote:
    I'm looking for help with a Windows keyboard problem. I need to be
    able to see individual keystrokes. I figured out how to do a kernel32
    SetConsoleMode() to change from line mode to character mode. This
    works well, except for the Esc key - which I need as part of the user
    interface.



    Windows seems to eat the Esc key. When I use os.StdinRead() or
    fmt.Scanf() nothing at all is returned. If I type Ctrl-[ however, I
    do get an escape character.
    I don't think that would work because this code is at the wrong layer:
    stdin (standard input) is a "cooked" stream of data which meant it has been "preprocessed" by the console implementation.

    Instead, you want to get hold on the "raw" state of the console.
    For this, there's no generic solution, and you'll have to resort to calling into Win32 API directly.

    See this [1] for more info. Basically, there two approaches are
    recommended:
    1) Plug your own handler into the message queue of the window
        hosting the console.
    2) Use a special (blocking) API call which is able to fetch most of
        keypresses (except for Ctrl, Pause etc).

    1. http://stackoverflow.com/q/2067893/720999

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

    For more options, visit https://groups.google.com/d/optout.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Khomoutov at Dec 1, 2014 at 1:34 pm

    On Sat, 22 Nov 2014 11:38:24 -0600 "John Souvestre" wrote:

    I was headed down that path, but came up with an easier way.

    // #include <conio.h>
    import "C"
    ...
    key := rune(C._getch())

    This seems to do the job for me.
    (Sorry for the late comment.)

    One problem with this approach is that your aplication starts to depend
    on the C runtime DLL, and to build it, your users will have to get a
    working C compiler (which, AFAIK, on Windows, is currently MinGW or
    something compatible). If you're fine with that, that's fine but if
    you intend for the package to be useful for a wider audience, resorting
    to cgo shouldn't be an easy consideration.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Khomoutov at Dec 1, 2014 at 1:50 pm

    On Mon, 1 Dec 2014 16:33:55 +0300 Konstantin Khomoutov wrote:
    I was headed down that path, but came up with an easier way.

    // #include <conio.h>
    import "C"
    ...
    key := rune(C._getch())

    This seems to do the job for me.
    (Sorry for the late comment.)

    One problem with this approach is that your aplication starts to
    depend on the C runtime DLL, and to build it, your users will have to
    get a working C compiler (which, AFAIK, on Windows, is currently
    MinGW or something compatible). If you're fine with that, that's
    fine but if you intend for the package to be useful for a wider
    audience, resorting to cgo shouldn't be an easy consideration.
    To make it may be more understandable, look at the popular package,
    odbc, which makes heavy use of the system DLLs purely using syscalls.
    This makes the package easily "go get-table" without requiring the users
    to have a working C compiler on their systems.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • John Souvestre at Dec 1, 2014 at 7:42 pm
    I agree with what you are saying, in general. The program I'm working on
    right now is something custom, which interfaces to some obsolete hardware. So
    the CGO solution doesn't bother me too much in this case.

    That being said, I also need some direct control over the console screen
    (cursor positioning). I'm thinking about using tarm/goserial. As it turns
    out, this package also contains low-level access to the keyboard, and doesn't
    use CGO.

    So it seems that tarm/goserial will nicely handle both functions for me. :)

    John

         John Souvestre - New Orleans LA

    -----Original Message-----
    From: Konstantin Khomoutov
    Sent: 2014 December 01, Mon 07:51
    To: John Souvestre
    Cc: Konstantin Khomoutov; golang-nuts@googlegroups.com
    Subject: Re: [go-nuts] Escape Key

    On Mon, 1 Dec 2014 16:33:55 +0300
    Konstantin Khomoutov wrote:
    I was headed down that path, but came up with an easier way.

    // #include <conio.h>
    import "C"
    ...
    key := rune(C._getch())

    This seems to do the job for me.
    (Sorry for the late comment.)

    One problem with this approach is that your aplication starts to
    depend on the C runtime DLL, and to build it, your users will have to
    get a working C compiler (which, AFAIK, on Windows, is currently MinGW
    or something compatible). If you're fine with that, that's fine but
    if you intend for the package to be useful for a wider audience,
    resorting to cgo shouldn't be an easy consideration.
    To make it may be more understandable, look at the popular package, odbc,
    which makes heavy use of the system DLLs purely using syscalls.

    This makes the package easily "go get-table" without requiring the users to
    have a working C compiler on their systems.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • RickyS at Dec 4, 2014 at 10:04 pm
    See about importing "code.google.com/p/go.crypto/ssh/terminal"
    Take a look at
    https://code.google.com/p/go/source/browse/?repo=crypto#hg%2Fssh%2Fterminal

    This is supposed to accomplish this, but I haven't tried it on Windows.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 21, '14 at 6:46p
activeDec 4, '14 at 10:04p
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase