Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39419 > unrolled thread

How can I read individual keystrokes?

Started byMartin Gregorie <martin@mydomain.invalid> (Martin Gregorie)
First post2020-04-05 00:17 +1200
Last post2020-04-05 11:22 +1200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How can I read individual keystrokes? Martin Gregorie <martin@mydomain.invalid> (Martin Gregorie) - 2020-04-05 00:17 +1200
    Re: How can I read individual keystrokes? Eric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman) - 2020-04-04 20:38 +1200
    Re: How can I read individual keystrokes? Arne Vajhøj <arne@vajhoej.dk> (Arne Vajhøj) - 2020-04-04 22:04 +1200
      Re: How can I read individual keystrokes? Martin Gregorie <martin@mydomain.invalid> (Martin Gregorie) - 2020-04-05 11:22 +1200

#39419 — How can I read individual keystrokes?

FromMartin Gregorie <martin@mydomain.invalid> (Martin Gregorie)
Date2020-04-05 00:17 +1200
SubjectHow can I read individual keystrokes?
Message-ID<1961251965@f38.n261.z1.binkp.net>
I have a need to read individual key presses as they are made. I'm developing 
this stuff under Linux with OpenJava 1.8 and would prefer a portable Java 
solution if that's possible.

I have a working solution, but its ugly. A small C program captures the one or 
more characters emitted as a key is pressed and sends them individually to my 
Java application over a local network session.

I've also tried using the Reader.read() method of the Reader instance returned 
by calling Console.reader() but that uses line buffering internally. As a 
result the Java code doesn't get sent anything until I hit RETURN, when all the 
characters typed since
Also, there's a good chance that the C program is using OS-specific features to 
let it capture individual keystrokes.

I have a feeling that I'm missing something obvious and so would appreciate it 
if any of you can point me in the direction of a pure Java way to capture 
individual key presses.


--
Martin    | martin at
Gregorie  | gregorie dot org

[toc] | [next] | [standalone]


#39420

FromEric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman)
Date2020-04-04 20:38 +1200
Message-ID<3174867@f38.n261.z1.binkp.net>
In reply to#39419
On 4/4/2020 8:17 PM, Martin Gregorie wrote:
> I have a need to read individual key presses as they are made. [...]
> I have a feeling that I'm missing something obvious and so would
> appreciate it if any of you can point me in the direction of a pure Java
> way to capture individual key presses.

     In a Swing setting this should be straightforward ("should be" --
hah!) even if somewhat verbose.  For a "console" app I'm not so sure, and for 
JavaFX I've no clue at all.

     Can you offer any details of your application?

--
esosman@comcast-dot-net.invalid
Trump wants to star as The Lyin' King. Two hundred ninety-one days to go.

[toc] | [prev] | [next] | [standalone]


#39421

FromArne Vajhøj <arne@vajhoej.dk> (Arne Vajhøj)
Date2020-04-04 22:04 +1200
Message-ID<1714594589@f38.n261.z1.binkp.net>
In reply to#39419
On 4/4/2020 8:17 PM, Martin Gregorie wrote:
> I have a need to read individual key presses as they are made. I'm
> developing this stuff under Linux with OpenJava 1.8 and would prefer a
> portable Java solution if that's possible.
>
> I have a working solution, but its ugly. A small C program captures the
> one or more characters emitted as a key is pressed and sends them
> individually to my Java application over a local network session.
>
> I've also tried using the Reader.read() method of the Reader instance
> returned by calling Console.reader() but that uses line buffering
> internally. As a result the Java code doesn't get sent anything until I
> hit RETURN, when all the characters typed since the last RETURN are sent,
> followed by the LF, so is of no use here.
>
> Also, there's a good chance that the C program is using OS-specific
> features to let it capture individual keystrokes.
>
> I have a feeling that I'm missing something obvious and so would
> appreciate it if any of you can point me in the direction of a pure Java
> way to capture individual key presses.

For a Swing app then it is easy: KeyListener.

For a console app it is a problem.

Some of the common workarounds are:
* use JNI and some OS specific C code
* use an existing library that does the above (like jCurses)
* use an OS specific command (stty on Linux) to put console
   in single character mode and use System.in.read()

Arne

[toc] | [prev] | [next] | [standalone]


#39422

FromMartin Gregorie <martin@mydomain.invalid> (Martin Gregorie)
Date2020-04-05 11:22 +1200
Message-ID<4164905457@f38.n261.z1.binkp.net>
In reply to#39421
On Sat, 04 Apr 2020 22:04:13 -0400, Arne VajhÄ,j wrote:

> On 4/4/2020 8:17 PM, Martin Gregorie wrote:
>> I have a need to read individual key presses as they are made. I'm
>> developing this stuff under Linux with OpenJava 1.8 and would prefer a
>> portable Java solution if that's possible.
>>
>> I have a working solution, but its ugly. A small C program captures the
>> one or more characters emitted as a key is pressed and sends them
>> individually to my Java application over a local network session.
>>
>> I've also tried using the Reader.read() method of the Reader instance
>> returned by calling Console.reader() but that uses line buffering
>> internally. As a result the Java code doesn't get sent anything until I
>> hit RETURN, when all the characters typed since the last RETURN are
>> sent,
>> followed by the LF, so is of no use here.
>>
>> Also, there's a good chance that the C program is using OS-specific
>> features to let it capture individual keystrokes.
>>
>> I have a feeling that I'm missing something obvious and so would
>> appreciate it if any of you can point me in the direction of a pure
>> Java way to capture individual key presses.
>
> For a Swing app then it is easy: KeyListener.
>
> For a console app it is a problem.
>
> Some of the common workarounds are:
> * use JNI and some OS specific C code * use an existing library that
> does the above (like jCurses)
> * use an OS specific command (stty on Linux) to put console
>    in single character mode and use System.in.read()
>
Thanks, guys: it looks as though I didn't miss anything.

I'm happy with using the C program as a keyboard listener knowing that there 
isn't a pure Java solution.

The application is, in fact, yet another Curses implementation(!). I looked at 
jCurses before I wrote it and decided to see how far I could get without diving 
into JNI and friends. So, first I wrote a from-the- manual JTermcap 
implementation and then buil
This was/is intended to be the basis of a 4GL written in Java and based heavily 
on the Sculptor 4GL. Why? Because writing applications with Sculptor is very 
fast and easy and it wouldn't adapt easily to using Swing. I think writing a 
set of Java file i/o 
Besides, years ago I wrote my own enhanced version of the Sculptor menu display 
program in C and using curses. That's  still in daily use on my Linux systems 
and on the OS-9 emulator I use to run Sculptor programs.


--
Martin    | martin at
Gregorie  | gregorie dot org

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web