• Post Reply Bookmark Topic Watch Topic
  • New Topic

Keyboard I/O  RSS feed

 
Rick Gentry
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a class that I want to input and output directly from the keyboard. I've got KeyListener implemented but I don't know what to add the key listener to so I can immediatley print whatever is typed.
For example: press 'a'->println('a')
What do I do? I've searched through the documentation to no success and every book I've seen doesn't seem to believe in keyboards.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no way to open a console in raw mode, so you can't do this at the command line. It's unfortunate, but true. KeyListeners are a GUI thing. You can add them to basically any GUI component, and then when that component has the focus, pressing the key will send an event to your listener.
 
Rick Gentry
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping that there was something I could make invisible and then interecept the KeyEvents from that....or is that possible?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure whether this will help you, because I only had a brief look at it:
Search google for 'charva' which is a project to convert swing- and awt- applications to console.
They might have a solution for your problem.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a class that used JNI to get fine-grained control of the console and one could use that to read characters from the keyboard and generate events, but there's still the problem that KeyEvents require a Component argument in their constructor. If you don't think spoofing that would be a problem I'd be glad to share the source (Windows-only, due to the JNI). You could always create your own event class...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!