java - Detecting backspace key press -
is there way detect when backspace key on keyboard has been pressed, using document filter? the following edited code extract here
for example
public class intfilter extends documentfilter { boolean truefalse = true; public void insertstring(documentfilter.filterbypass fb, int offset, string string, attributeset attr) throws badlocationexception { stringbuffer buffer = new stringbuffer(string); (int = buffer.length() - 1; >= 0; i--) { char ch = buffer.charat(i); if (!character.isdigit(ch)) { buffer.deletecharat(i); truefalse = false; } /* else if (backspace pressed) { truefalse = true; } */ else{ truefalse = true; } } super.insertstring(fb, offset, buffer.tostring(), attr); } public void replace(documentfilter.filterbypass fb, int offset, int length, string string, attributeset attr) throws badlocationexception { if (length > 0) fb.remove(offset, length); insertstring(fb, offset, string, attr); } }
pressing backspace key won't trigger insertstring()
method. should rather trigger remove()
method (only when text removed, not case when caret @ beginning of text example).
but can detect keystroke using keylistener
(doc). here's how detect backspace key:
public class keyeventdemo implements keylistener { /** handle key typed event text field. */ public void keytyped(keyevent e) {} /** handle key-pressed event text field. */ public void keypressed(keyevent e) {} /** handle key-released event text field. */ public void keyreleased(keyevent e) { if(e.getkeycode()==keyevent.vk_back_space){ // something... } } }
Comments
Post a Comment