The Artima Developer Community
Sponsored Link

Java Answers Forum
How to Recognise a Typed Key?

2 replies on 1 page. Most recent reply: Jan 2, 2006 5:14 AM by Will

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Will

Posts: 20
Nickname: lofty
Registered: Mar, 2005

How to Recognise a Typed Key? Posted: Jan 2, 2006 1:19 AM
Reply to this message Reply
Advertisement
Hi,

I've just come back to a project that I was struggling with before - and I still am. I'm trying to use KeyListener to compare a typed or pressed key, so that if a certain key is pressed, a method is invoked.

I have added KeyListener to 2 text fields, and using the getSource() method, I can at least tell which text field the key stroke occurred in. What I need to be able to do now, is say that if that key stroke = "d" or "D", I want to enter todays date in the text field.

The date part is no problem, but I'm having a heck of a job trying to recognise and compare the pressed key. Am I on the right lines with getKeyChar() or am I going completely wrong?

Any help would be very much appreciated - this is a personal project, not homework or anything. I haven't posted any code because ther than adding the listener to the textfield and invoking the getSource() method, there isn't a lot to see.

Many thanks!


Will

Posts: 20
Nickname: lofty
Registered: Mar, 2005

Re: How to Recognise a Typed Key? Posted: Jan 2, 2006 5:12 AM
Reply to this message Reply
OK, I've figured out how to do this, but I still have a problem. I'm using the following:

if(e.getKeyCode()==KeyEvent.VK_D){
//etc
}

Which works fine, and I've got the date added - the problem is that I now have the D appended to the date format.

I've tried textField.setText("") followed by adding the date, but it still appends the typed key. Can anyone help me here?
Thanks.

Will

Posts: 20
Nickname: lofty
Registered: Mar, 2005

Re: How to Recognise a Typed Key? Posted: Jan 2, 2006 5:14 AM
Reply to this message Reply
Oh, ahem. I was using the key pressed method - key released works just fine ;)

Flat View: This topic has 2 replies on 1 page
Topic: connection with wap gateway Previous Topic   Next Topic Topic: connection java application to MySQL Database

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use