Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to capture the delete key?

 
danish shaikh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I m creating an TextEditor having the track changes feature as in MSWord 2003 ie whenever i make ne changes in the document the changes r highlighted with different color.... also whenever i delete some words it shud be displayed as striked out... this is working for backspace key but not for the delete key... if have used ke.consume() function to capture the delete/backspace key event... its works fine for backspace but not for delete... so plz do the needful its very urgent...

Looking for ur reply...

Regards,
danish shaikh.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a KeyListener. Use KeyEvent.getKeyCode() == KeyEvent.VK_DELETE to trap when the delete key is pressed.
D.
 
danish shaikh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for reply, but i have already used the same logic but it is not working my logic is:--

public void keyPressed (KeyEvent ke)
{
int code=ke.getKeyCode();
if(code==KeyEvent.VK_DELETE && flag==true)
consume=true;// used to consumed the key in the keytyped event
}

thanks
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works for me. Are you sure flag isn't false ?
 
danish shaikh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,,
thanks for reply.
i m damm sure that the flag will become false.
flag is used to check the existing file is selected or not (true).
The same logic is used for backspace but its working ...
public void keyPressed(KeyEvent ke)
{
int code=ke.getKeyCode();
if(code==KeyEvent.VK_BACK_SPACE && flag==true)
consume=true;
}

thnks in advance,
Bye.Regards,
Danish Shaikh
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you debug your code. Print out the value of code and flag in your keyPressed method - what is printed ?
D.
 
danish shaikh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to debug the code i m using Textpad as n editor ...
so i dont know how to do debugging..

Thanks.
Bye,
Regards,
Danish Shaikh
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You add calls to System.out.println and run the code.
D.
 
Josiah Yoder
Greenhorn
Posts: 1
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found in my debugging

(1) That I needed to add my action listener to the component I'm listening to... (I always forget this)
(2) I need to use KeyEvent.VK_DELETE == e.getKeyChar() instead of KeyEvent.VK_DELETE == e.getKeyCode()

Then it worked for me.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Some new people wait a long time before posting; I think the longest I have seen is a full ten years. But you have gone the other way, finding the solution after eleven years!
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While we are on the topic of listening to key input, the recommended way is to use Key bindings
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic