Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts

    KeyEvent.getKeyChar issues for ctrl-j and ctrl-m

    Hi,

    I have an application that is primarily keyboard driven. I was adding a new keyboard action to the application and came across something I wasn't aware of.

    If I print out the KeyEvent.getKeyChar() after pressing Ctrl-m the newline character (\n) is returned. Or since the application treats the char as an int, 10 is returned.

    Interestingly, when pressing Ctrl-j, KeyEvent.getKeyChar() returns the newline character as well.

    Confused by this I opened notepad and pressed ctrl-m and then ctrl-j and noticed that they both add a newline to the file.

    Does anyone have any tips on how I might differentiate between ctrl-m and ctrl-j in Java?

    Edit: I've done some further research that I feel I should mention
    The above issues of not being able to differentiate between ctrl-j and ctrl-m occurs if checking the KeyEvent in the KeyTyped method. One can differentiate if they check the KeyEvent in the KeyPressed method.

    Ctrl-j
    KeyTyped: java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar=Enter,modifiers=Ctrl,extModifiers=Ctrl,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primar yLevelUnicode=0,scancode=0]
    KeyPressed: java.awt.event.KeyEvent[KEY_PRESSED,keyCode=74,keyText=J,keyChar=Enter,modifiers=Ctrl,extModifiers=Ctrl,keyLocation=KEY_LOCA TION_STANDARD,rawCode=74,primaryLevelUnicode=106,scancode=36]

    Checking in the KeyPressed method has been deemed not valid (By Authorities greater than me), so I'm still looking for a workaround when checking the KeyEvent in the KeyTyped method.


    Thanks,
    Gox
    Last edited by Gox; 01-07-2011 at 06:53 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    KeyTyped events don't return their codes in the KeyTyped method (they return VK_UNDEFINED), so I'm not sure there is a solution to finding the ctrl + j press.

    We know that the keypressed and keyreleased both toss the keycode from them. Perhaps you can use a combination of the keyPressed and keyReleased together to simulate the keyTyped event? Actually, I wonder if you can tie it back directly; unfortunately I'm no gui expert so I don't know what implications could arise from this, but I wonder if this will work:
    PHP Code:
    component.addKeyListener(new KeyAdapter()
    {
        public 
    void keyTyped(KeyEvent e)
        {
            
    super.keyReleased(e);
        }

        public 
    void keyReleased(KeyEvent e)
        {
            
    super.keyReleased(e);
            
    System.out.println(e.getKeyCode());
        }
    }); 
    ?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Fou-Lu thanks for the response.

    You are correct, if I follow your idea the KeyCode does seem to be preserved and unique. I can differentiate between ctrl-j and ctrl-m. However, I have doubts that this proposal will satisfy the parties involved; for the same reasons checking in keyPressed was rejected. Again, I appreciate the idea and will see what I can do with it.

    If the meantime if there are any other thoughts I'd love to hear them.
    Last edited by Gox; 01-07-2011 at 08:09 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Hmm, maybe check in on the KeyMap class. Unfortunately, I have never once touched this class, so I don't know how much help I can be. What it *looks* like it will do is allow you to specify particular actions independent of the keyListener and bind them to the KeyStroke class. Sweet if that works!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Gox (01-07-2011)

  • #5
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    I will look into this, thanks for the thoughts. Much appreciated.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Gox View Post
    I will look into this, thanks for the thoughts. Much appreciated.
    You betcha mate, keep an update too to let us know how its coming along
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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