Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in Shift button keyPressed event  RSS feed

 
Avani Joshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem during the shift key presse event on the text box. Whenever I press Shift + 8 key the key that is displayed is Shift + *. But I want Shift + 8 to be displayed. Can anyone please tell me the solution to this problem.

- Thanks in advance
[ October 08, 2008: Message edited by: Rob Prime ]
 
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
Originally posted by Avani Joshi:
Hi,
I have a problem during the shift key presse event on the text box. Whenever I press Shift + 8 key the key that is displayed is Shift + *. But I want Shift + 8 to be displayed. Can anyone please tell me the solution to this problem.

- Thanks in advance


What do you mean Shift+* is "displayed". Are you trapping and processing the key events? If so, how? There is no KeyEvent.VK_* . You should be trapping and processing it based on KeyEvent.VK_8
I want S**t + 8 to be displayed

This one made me laugh
[ October 08, 2008: Message edited by: Rob Prime ]
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed the typo. It can be offensive to people you know.

As Maneesh said, there is no * key. On most keyboards * simply is Shift+8 and nothing else. On other keyboards it will be a similar combination.
 
Avani Joshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you both have miss understtod my question. Maybe I have not explained my problem properly. I will try again.

As in all applications we have short cut keys like Ctrl+F for find dialog to open. Like wise I want these shortcut keys to be user defined in my application. The user will get a window wherein he can can enter his own short cut key for a particular operation to be performed.

I am doing the above functionality in Swing. So I have used a textbox for the user to enter his desired key. Using the keypress event of the textbox I am able to capture all the keys entered by the user and display it on the textBox. I am capturing the Ctrl, Alt and Shift keys using the isControlDown() etc of the InputEvent class.

Now the problem that I am facing is whenever the user presses Shift and 5 simulatenously the text that is getting on the textbox is Shift+%. This is the case with Shift key onl but when the user presses Ctrl+5 the text that is displayed is Ctrl+5. The problem with Shift is that it behaving according to its default behaviour.

Is there any way I can override this behaviour so that when user presses Shift and 5 simultaneously the text that is placed is displayed is Shift+5 and not Shift+%.

I hope that I have made my problem clear. Please let me know if you have any solution to this problem.

-Thanks in advance
[ October 10, 2008: Message edited by: Avani Joshi ]
 
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
Originally posted by Avani Joshi:
Is there any way I can override this behaviour so that when user presses Shit and 5 simultaneously the text that is placed is displayed is Shift+5 and not Shift+%.


Easiest approach would be to intercept the key events, process them, and then set the appropriate text in the text box.
 
Avani Joshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried processing it but still it does not work.
I have tried using the following if condition

// if the shift key is pressed
if (e.isShiftDown()) {
// if its numeric value from 0 to 9
if (keyCode >= 48 && keyCode <= 57) {
jTextField1.setText("Shift+ " + KeyEvent.getKeyText(keyCode));

The output i am getting is Shift+5% . It does print Shift+5 but along with the special character. I am not getting any way of removing the special character. I have tried hardcoding the values but still it giving the same results.
Is there any other way for doing the same?

-Thanks in advance

[ October 10, 2008: Message edited by: Avani Joshi ]
[ October 10, 2008: Message edited by: Avani Joshi ]
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Avani Joshi:
I if (keyCode >= 48 && keyCode <= 57)

You can also use KeyEvent.VK_0 and KeyEvent.VK_9 instead of 48 and 57 for clarity. Also, '0' and '9' (the characters) can be used.

The output i am getting is Shift+5% . It does print Shift+5 but along with the special character. I am not getting any way of removing the special character. I have tried hardcoding the values but still it giving the same results.
Is there any other way for doing the same?

Can't you make the text field uneditable?
 
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
Also check out
 
Avani Joshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maneesh and Rob for your help. The solution of making the textfield uneditable is working for me. Once again thanks a lot for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!