Results 1 to 6 of 6

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.


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

    logic (15th December 2014)

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.