Results 1 to 6 of 6

Thread: QML Key Press Events break when multiple keys are pressed

  1. #1
    Join Date
    Dec 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default QML Key Press Events break when multiple keys are pressed

    Hey everyone, I'm having an issue with my keyPress events. My problem happens when I hold down one key, and press another (not held) at the same time. Basically, think of playing Super Mario where you're running left, right, and jumping. Well, my character moves left, right and jumps... but if I'm moving right or left and press jump, my character stops moving left or right.

    Now, I've looked into my code and can only conclude it's a problem in the key events. This is my code:

    Qt Code:
    1. Keys.onPressed: {
    2.  
    3. if (event.key == Qt.Key_Left) {
    4. console.log("Left")
    5. player.moveHoriz = -1;
    6. player.moveBackward();
    7.  
    8. }
    9. else if (event.key == Qt.Key_Right) {
    10. console.log("Right")
    11. player.moveHoriz = 1;
    12. player.moveForward();
    13.  
    14. }else if (event.key == Qt.Key_Up) {
    15. console.log("Jump")
    16. player.jump();
    17. }
    18. } Keys.onReleased: {
    19.  
    20. if (event.isAutoRepeat)
    21. return ;
    22.  
    23. if (event.key == Qt.Key_Left ||
    24. event.key == Qt.Key_Right) {
    25. player.moveHoriz = 0;
    26.  
    27. return ;
    28. }
    29.  
    30. //if (event.key == Qt.Key_Up){
    31. // return ;
    32. //}
    33. }
    To copy to clipboard, switch view to plain text mode 

    If I run my game in a console, this is what is happening behind the scenes.



    As you can see, my held-key event is broken upon jumping. I have to take my finger off of my held key (such as the right arrow) and press it again to start moving right once again. I know that QML is not the best tool for building games in, but I'm sure it can't be this limited in key event control. If it makes a difference, I'm on Ubuntu 14.04 - Linux Kernel 3.13.0-43-generic.

    Thanks for the help.
    Last edited by logic; 14th December 2014 at 21:17.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QML Key Press Events break when multiple keys are pressed

    Your app behaves the way you told it to. If you press a key, you get a key event with that key in the event's key property. Then you get more events for that key because of autoRepeat being active, hence your event handlers fire again. There is no logic in your code for handling keys which are already pressed when a new event arrives. A regular approach to handling key events in games is to have a set of flags which are raised or lowered based on incoming events.

    javascript Code:
    1. SomeElement {
    2. id: root
    3. // ...
    4.  
    5. property bool isMovingRight: false
    6.  
    7. Keys.onRightPressed: {
    8. if(event.isAutoRepeat) return
    9. isMovingRight = true
    10. }
    11. Keys.onReleased: {
    12. if(event.isAutoRepeat) return
    13. if(event.key === Qt.Key_Right) isMovingRight = false
    14. }
    15.  
    16. states: [
    17. State {
    18. name: "characterMovingRight"
    19. when: root.isMovingRight
    20. }
    21. ]
    22. }
    To copy to clipboard, switch view to plain text mode 

    By the way:

    I know that QML is not the best tool for building games in
    Where exactly do you know that from? My impression is exactly opposite to that of your source of information.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    logic (15th December 2014)

  4. #3
    Join Date
    Dec 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QML Key Press Events break when multiple keys are pressed

    Okay, your explanation did help give me better results. I can now jump and move at the same time and continue moving. I utilized a Timer and set a flag on the player. While this does make things a lot smoother. My next issue is this (and I still find it's related to the key events). If I'm holding down the right key and my character goes to the right, AND THEN I hold the left key down, my character starts moving left (that's fine). If I let go of the left key, my flag resets to zero instead of catching that the right key is still held down and it should go back to 1 (-1 = left, 1 = right). It's only a real problem if I'm trying to slow my character down by "tapping" the opposite direction. I don't know how to accomplish this with the release events since I don't want a "tap" to keep me from continuing in the same direction. I appreciate the help, thank you very much.

    Qt Code:
    1. Keys.onLeftPressed: {
    2. if(event.isAutoRepeat) return;
    3. player.moveHoriz = -1;
    4. player.startTimer();
    5. }
    6. Keys.onRightPressed: {
    7. if(event.isAutoRepeat) return;
    8. player.moveHoriz = 1;
    9. player.startTimer();
    10. }
    11. Keys.onUpPressed: {
    12.  
    13. player.jump();
    14. }
    15.  
    16.  
    17.  
    18.  
    19. Keys.onReleased: {
    20.  
    21. if (event.isAutoRepeat)
    22. return ;
    23.  
    24. if (event.key == Qt.Key_Left &&
    25. event.key == Qt.Key_Right) {
    26. player.moveHoriz = -player.moveHoriz;
    27. return ;
    28. } else if (event.key == Qt.Key_Left || event.key == Qt.Key_Right){
    29. player.moveHoriz = 0;
    30. }
    31.  
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QML Key Press Events break when multiple keys are pressed

    Flags for right and left should be exclusive. Setting one should clear the other.

    By the way the condition from lines 24 and 25 will never evaluate to true.

    Also try doing things more in declarative manner. In particular I don't like the timer of yours.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    logic (15th December 2014)

  7. #5
    Join Date
    Dec 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QML Key Press Events break when multiple keys are pressed

    Thank you. I actually did get it to work once I set exclusive flags. I feel stupid, my mistake. I don't know what to use other than a timer, though I don't have it starting upon key events at this point. I use the timer to control the incremental acceleration, but if there is a better way, I wouldn't mind hearing it.

    Thank you for the help, though. I actually have the fundamentals of the controls I need now. Anyway, here's what my code looks like now and it's definitely working:

    Qt Code:
    1. Keys.onLeftPressed: {
    2. if(event.isAutoRepeat) return;
    3. player.moveHoriz = -1
    4. player.moveLeft = true;
    5. }
    6. Keys.onRightPressed: {
    7. if(event.isAutoRepeat) return;
    8. player.moveHoriz = 1;
    9. player.moveRight = true;
    10. }
    11. Keys.onUpPressed: {
    12.  
    13. player.jump();
    14. }
    15.  
    16.  
    17.  
    18.  
    19. Keys.onReleased: {
    20.  
    21. if (event.isAutoRepeat)
    22. return ;
    23.  
    24. if(event.key == Qt.Key_Left){
    25. player.moveLeft = false;
    26. }
    27. if(event.key == Qt.Key_Right){
    28. player.moveRight = false;
    29. }
    30.  
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    In regards to where I had heard QML wasn't great for games, it was from a developer I knew who builds on Windows. I think he feels the performance is lackluster, but I don't know what his personal gripes with it might be. Anyway, it hasn't stopped me from working with it and that's why I came here to find out how to do what I needed to do. As I said, thanks for the help. I don't often come to forums, but this was more than helpful.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QML Key Press Events break when multiple keys are pressed

    Instead of a timer you could use a Behavior or Animation (or both). But even with a timer you should state when the timer is active instead of manually starting and stopping it. Don't try replicating imperative approach in a declarative environment. This leads to clutter in code and less performant result.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Receiving events while mouse is pressed
    By davj in forum Qt Programming
    Replies: 4
    Last Post: 21st May 2014, 08:11
  2. Want to detect keyboard key press other than modifier keys
    By Rajesh.Rathod in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2013, 07:19
  3. I press a button and receive 2 pressed() signals
    By JuanMO in forum Qt Programming
    Replies: 4
    Last Post: 21st November 2010, 06:58
  4. Get pressed keys (no modifiers) from QMouseEvent
    By mariposa in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2010, 11:44
  5. Catch pressed keys
    By jano_alex_es in forum Newbie
    Replies: 5
    Last Post: 30th July 2009, 12:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.