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


Groups > comp.lang.java.programmer > #39417

Re: How can I read individual keystrokes?

Path csiph.com!aioe.org!.POSTED.O5hsyUvK01te0L/SZurVSQ.user.gioia.aioe.org!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: How can I read individual keystrokes?
Date Sat, 4 Apr 2020 22:04:13 -0400
Organization Aioe.org NNTP Server
Lines 34
Message-ID <r6beb1$1sef$1@gioia.aioe.org> (permalink)
References <r6b830$hra$1@dont-email.me>
NNTP-Posting-Host O5hsyUvK01te0L/SZurVSQ.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0
Content-Language en-US
X-Notice Filtered by postfilter v. 0.9.2
Xref csiph.com comp.lang.java.programmer:39417

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web