Results 1 to 4 of 4
  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default SOLVED: Intercept key type

    I tried using the following code to intercept when the user types the Delete key.

    Java Code:
    myJList.addKeyListener(new KeyListener() {
        void keyTyped(KeyEvent evt) {
            if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
                // action code
            }
        }
    });
    The if statement never returns true though, as it seems the getKeyCode() keeps returning only 0, regardless of what I type. If E remove the if statement then it runs - but for any key...

    Help is appreciated.
    Last edited by carderne; 07-23-2009 at 08:33 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    See the third paragraph in the KeyEvent comments section: keyTyped events are not generated for non-Unicode character keys. Try keyPressed.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Or use key bindings here instead.

  4. #4
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Quote Originally Posted by hardwired View Post
    See the third paragraph in the KeyEvent comments section: keyTyped events are not generated for non-Unicode character keys. Try keyPressed.
    Oh thanks I stupidly didn't spot that. I assumed that keyTyped meant that the key was pressed and released. So probably SOLVED...

Similar Threads

  1. Intercept CVS/SVN team commit
    By dilipg003 in forum Eclipse
    Replies: 2
    Last Post: 05-12-2009, 07:17 PM
  2. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 11:43 AM
  4. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •