regarding textinput and arrow keys



  • hello ,

    im entering characters through arrow keys, up and down arrow keys for entering values to textinput, left and right arrow keys for cursor navigation, i have a sample code, producing weird results, help me out

    i have made use of js file also,

    import QtQuick 1.1
    
    import "Script.js" as Script;
    
    
    Rectangle {
        width: 360
        height: 360
    //    property alias textinput : textinput.text
        Rectangle
        {
            id: rect
            width: 150
            height: 30
            border.color: "black"
            border.width: 2
            anchors.centerIn: parent
            focus: true
            TextInput
            {
                id: textinput
                focus: true
                width: parent.width
                height: parent.height
                cursorVisible: true
                anchors.centerIn: parent
            }
        }
    
        Keys.onUpPressed:
        {
            Script.asciiCodedecrement()
            event.accepted = true
        }
    
        Keys.onDownPressed:
        {
            Script.asciiCodeincrement()
            event.accepted = true
        }
    
        Keys.onLeftPressed:
        {
            Script.moveLeft()
            event.accepted = true
        }
    
        Keys.onRightPressed:
        {
            Script.moveRight()
            event.accepted = true
        }
    
    }
    

    Script.js

    var counter=0
    var text;
    var arrayString=[];
    
    function asciiCodeincrement()
    {
        var i= textinput.text.charCodeAt(textinput.cursorPosition);
        console.log("*********value of decrement i=**********",i);
        if(i===0)
        {
            if(textinput.cursorPosition===0)
            {
                i=65;
                text = String.fromCharCode(i);
                console.log("text name1:",text)
            }
        }
        else{
            if((i>=65 && i<90)||(i>=97 && i<122)|| (i>=48 && i<57))
            {
                i++;
                text = String.fromCharCode(i);
                console.log("text name2:",text)
            }
            else
                text = String.fromCharCode(i);
            console.log("text name3:",text)
        }
        if(i===90)
        {
            i=i+7
            text = String.fromCharCode(i);
            console.log("text name4:",text)
        }
        if(i===122)
        {
            i=i-74
            text= String.fromCharCode(i);
            console.log("text name5:",text)
        }
        if(textinput.cursorPosition!=0)
        {
            textinput.text =(arrayString[textinput.cursorPosition-1]+text);
        }
        else
            textinput.text=text
        textinput.cursorPosition = textinput.text.length-1
    }
    
    
    function asciiCodedrecrement()
    {
        var i=textinput.text.charCodeAt(textinput.cursorPosition);
        console.log("*********value of increment i=**********",i);
        if((i>65 && i<=90) || (i>97 && i<=122)||(i>48 && i<=57))
        {
            i--;
            text=  String.fromCharCode(i);
            console.log("text name11",text)
        }
        else if(i===97)
        {
            i=i-7
            text= String.fromCharCode(i);
            console.log("text name2",text)
        }
        else if(i===48)
        {
            i=i+74
            text= String.fromCharCode(i);
            console.log("text name3",text)
        }
        else
            text = String.fromCharCode(i);
    
        if(textinput.cursorPosition!=0)
        {
            textinput.text =(arrayString[textinput.cursorPosition]+text);
            console.log("text name4",textinput.text);
        }
        else
        {
            textinput.text=String.fromCharCode(i)
            textinput.cursorPosition = textinput.text.length;
            console.log("cursor position",textinput.cursorPosition);
        }
    }
    
    function moveRight()
    {
        arrayString[counter] = textinput.text;
        console.log(arrayString[counter])
        textinput.cursorPosition=textinput.text.length;
        console.log(textinput.cursorPosition)
        if(textinput.cursorPosition!=0)
            textinput.text=arrayString [textinput.cursorPosition-1]+'A';
        else
            textinput.text='A'
        textinput.cursorPosition=textinput.text.length-1;
        counter++
    }
    
    function moveLeft()
    {
        if( textinput.cursorPosition>=1)
        {
            textinput.cursorPosition--
            counter--
        }
    }
    

  • Moderators

    @Pradeep-Kumar
    First most important please make use of code markers while posting code blocks. Otherwise it is difficult to read. I have again done it for you this time.
    Now your questions:

    i am stuck up with a problem, hope u'l help me, i have four textinput vertically alligned and on Keys.onreturnpressed how to navigate to all four textinput with enter keys, give me the logic please.

    There are few ways to do that. You can keep a property through which you wll be able to cycle through multiple TextInput. Change this property from your Keys.onReturnPressed handler. For eg: consider this example,try running it

    import QtQuick 2.5
    Rectangle {
        width: 200
        height: 400
    
        property int currentTextInputItem : 0
        focus: true
    
        Column {
            TextInput {
                focus: currentTextInputItem==0 ? true : false
                width: 200
                text: "red"
                color: "red"
            }
            TextInput {
                focus: currentTextInputItem==1 ? true : false
                width: 200
                text: "green"
                color: "green"
            }
            TextInput {
                focus: currentTextInputItem==2 ? true : false
                width: 200
                text: "blue"
                color: "blue"
            }
            TextInput {
                focus: currentTextInputItem==3 ? true : false
                width: 200
                text: "purple"
                color: "purple"
            }
        }
    
        Keys.onReturnPressed: {
            currentTextInputItem===3 ? currentTextInputItem=0 : ++currentTextInputItem
        }
    }
    
    

    i want to enter to textinput via up and down arrow keys, please help me out

    How are you planning to do this ? i.e allowing to enter alphabets via these 2 keys.



  • yes im tryind to enter alphabet and numbers from two keys, and cursor navigation from two keys,


  • Moderators

    @Pradeep-Kumar And what is this component which will contain those alphabets ?



  • @p3c0

    converting ascii to characters in function itself in js file, with respect to conditions , u can look in the example posted,

    im trying it out in my pc also,


  • Moderators

    @Pradeep-Kumar You need to point out what you think is wrong rather than posting the whole code and asking to others for finding out the bug. Narrow down the problem. May be you yourself can find out the problem in your logic.



  • @p3c0

    kkk


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.