Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. regarding textinput and arrow keys

regarding textinput and arrow keys

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 2 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Pradeep KumarP Offline
    Pradeep KumarP Offline
    Pradeep Kumar
    wrote on last edited by p3c0
    #1

    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--
        }
    }
    

    Pradeep Kumar
    Qt,QML Developer

    p3c0P 1 Reply Last reply
    0
    • Pradeep KumarP Pradeep Kumar

      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--
          }
      }
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @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.

      157

      1 Reply Last reply
      0
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by
        #3

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

        Pradeep Kumar
        Qt,QML Developer

        p3c0P 1 Reply Last reply
        0
        • Pradeep KumarP Pradeep Kumar

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

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

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

          157

          Pradeep KumarP 1 Reply Last reply
          0
          • p3c0P p3c0

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

            Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            @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,

            Pradeep Kumar
            Qt,QML Developer

            p3c0P 1 Reply Last reply
            0
            • Pradeep KumarP Pradeep Kumar

              @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,

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @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.

              157

              Pradeep KumarP 1 Reply Last reply
              0
              • p3c0P p3c0

                @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.

                Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by
                #7

                @p3c0

                kkk

                Pradeep Kumar
                Qt,QML Developer

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved