• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Implementing KeyListener interface

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My simple text editor implements KeyListener interface.
As already known, it contains three metods to override:



In the implementation of keyTyped I put some code that indicates a text content has been changed (show asterisk on corresponding tab, enable Save menu item and toolbar button, ...).
What are other two methods for?
Since I have a status bar with CAPS LOCK indicator, i suppose I should put implementation for that in one of these. Is there any rule that says which keys events should be processed with these methods?
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the quite detailed explanation in the API docs for KeyEvent
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I got it.
This is what I did:


Here is a method for CapsLock indicator:

But when I try to do similar thing with Insert/Overwrite with:

I get exception:
 
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But when I try to do similar thing with Insert/Overwrite with:



And what does the getLockingKeyState() method say about that KeyEvent?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh... another mistake!

Valid key codes are VK_CAPS_LOCK, VK_NUM_LOCK, VK_SCROLL_LOCK, and VK_KANA_LOCK.



So, is there any other way to check if INSERT is on?
 
Rob Camick
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not a state for the entire keyboard. It is a state for individual applications, which means each application is responsible for tracking it.

Use Key Bindings to handle an Action when that key is preseed. You need to keep a boolean class variable. Every time the key is pressed you toggle its state. Then you application needs to query the boolean variable state as required.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this is my solution.


@Rob: Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!