This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem in Shift button keyPressed event

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check out
 
Avani Joshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yup, yup, yup. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic