1. #1
    Sencha User
    Join Date
    May 2011
    Posts
    27
    Vote Rating
    1
    sharaf.satishkumar is on a distinguished road

      0  

    Default Unanswered: TextField got messed up on Overriding KeyDown event.

    Unanswered: TextField got messed up on Overriding KeyDown event.


    Hi,
    I have overrided KeyDown Event am facing issues when i select some text and enter some charachter.
    1) In Normal case when we select some part off text with mouse and enter new charachter it should replace the whole selected text with new newly entered charachter, this not working poperly. TO OVER COME THIS, I wrote this peice of code .

    @Override
    public void componentKeyDown(final ComponentEvent event)
    {
    int charCode =event.getKeyCode();
    if((charCode > 64 && charCode < 91)&& !event.isControlKey()){
    char c = (char)charCode;
    String specialChar = null;
    specialChar = (""+c).toUpperCase();
    String specialString= "$"+specialChar;
    event.preventDefault();// if i wont write this line we will get the special character both small and Big(like aA, bB, cB...)
    TextField textField = (TextField) event.getComponent();
    String feildValue = (String) (textField).getValue();
    String selectedString =(textField).getSelectedText();
    int lengthOfSelected=(textField).getSelectionLength();
    int pos = (textField).getCursorPos();
    if(null!=feildValue){
    if(feildValue.length()<250){
    if(!selectedString.isEmpty()){
    feildValue = feildValue.substring(0, pos) + specialString + feildValue.substring (pos+lengthOfSelected, feildValue.length());
    }
    else{
    feildValue = feildValue.substring(0, pos) + specialString + feildValue.substring(pos, feildValue.length());
    }
    }
    else if(feildValue.length()==textField.getMaxLength()&&lengthOfSelected>0)
    {
    feildValue = feildValue.substring(0, pos) + specialString + feildValue.substring(pos+lengthOfSelected, feildValue.length());
    }
    textField.setValue(feildValue);
    textField.setCursorPos(pos+1);
    }

    }
    else
    {
    super.componentKeyDown(event);
    }
    }

    After writing this code i am facing new issue only with ie9 as, when we keep on entering charachters more than the visible length of text field. the charachters should move themself to left so that newly entered charachter can been seen alon with cursor but the charachter and cursor are getting entered under the textfield's right boundary and being hidden under it.
    Please help me out..

  2. #2
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,731
    Answers
    109
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    This looks more like a bug in your code, than a bug in GXT, so I'm moving it to Q&A.

    The method preventDefault is *supposed* to be called when you want to stop the browser from entering the key that the user actually typed - this is how that method works. If you don't call it, you should expect the normal key to also get typed.

    The main issue you are asking about sounds like it could be some odd behavior in IE9 - does it happen in any other browsers? What browsers behave as you would expect? Can you make a simple runnable example so others can try to test and tinker with this?

  3. #3
    Sencha User
    Join Date
    May 2011
    Posts
    27
    Vote Rating
    1
    sharaf.satishkumar is on a distinguished road

      0  

    Default please see the code attached.

    please see the code attached.


    Hi, this text going behind the text field issue is happening only in ie9 not in firefox, since 6 to 8 days , before that i had same piece of code working properly.

    Now coming to issues with key down event.
    I am providing the file attached which i had over-rided keyDown event.
    i have commented code some line in it. to cover all the the issues i found as below.


    1) // event.preventDefault();
    without this line we get the default key storke which is small letter and uppercase letter which i added in the code. so i prevented browser defaults behaviour then i get only the Capital letter for the key entered.

    2)// String selString =textField.getSelectedText();
    // int posSel=textField.getSelectionLength();
    // int pos = textField.getCursorPos();
    if(null!=value){
    // if(value.length()<textField.getMaxLength()){
    // if(!selString.isEmpty()){
    // value = value.substring(0, pos) + ucase + value.substring(pos+posSel, value.length());
    // }
    // else{
    value = value.substring(0, pos) + ucase + value.substring(pos, value.length());
    // }
    // }
    // else if(value.length()==textField.getMaxLength()&&posSel>0)
    // {
    // value = value.substring(0, pos) + ucase + value.substring(pos+posSel, value.length());
    // }


    without these lines we will get get the issue like,
    when we select a part of text with mouse and enter some letter, we expected that part of selected text should be chopped off and the newly entered letter should be appended. BUT, the selected text is not getting chopped of and newly entered charachter is added at new cursor position.
    Please, uncommented line which i had commented and run. You will get some idea how i have implemented it to over some selection issue.

  4. #4
    Sencha User
    Join Date
    May 2011
    Posts
    27
    Vote Rating
    1
    sharaf.satishkumar is on a distinguished road

      0  

    Default file attached

    file attached


    file attached
    Attached Files

  5. #5
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,731
    Answers
    109
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    I might not have been clear in my first post, so trying again:
    1) // event.preventDefault();
    without this line we get the default key storke which is small letter and uppercase letter which i added in the code. so i prevented browser defaults behaviour then i get only the Capital letter for the key entered.
    This is correct behavior. You need to call preventDefault in this case. You should have to do this, or you should get double keystrokes. This is correct code.


    This only leaves the second issue - and if it works with that code, why are you commenting it out?

  6. #6
    Sencha User
    Join Date
    May 2011
    Posts
    27
    Vote Rating
    1
    sharaf.satishkumar is on a distinguished road

      0  

    Default


    Hi Colin.
    Please Over ride Key Down Event event for converting each Upcoming alphabet to capital Letter.

    After Over riding, please Select a part of the text and enter an alphabet, you will face some abnormality text field behavior. ( The commented code which i mentioned in 2nd issue,is to avoid this abnormality after selection, please Uncomment that peice and Enter an alphabet. I have not commented that code in my implementation. I gave that code for illustrating Admin)

Thread Participants: 1