Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing KeyListener interface  RSS feed

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • 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?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 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
Java Python Ruby
  • 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:
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • 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
Java Python Ruby
  • 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
Ranch Hand
Posts: 2787
12
  • 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
Java Python Ruby
  • 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!