Results 1 to 9 of 9

Thread: Capture a keyboard event

  1. #1
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Capture a keyboard event

    Hi all,

    Can anybody help me to know how to capture keyboard input.

    QKeyEvent class i refered. But i want to know how to initialize it to capture an event.

    Plz help...

    Mahe2310

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Capture a keyboard event

    use eventFilter() or keyPressEvent()
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Capture a keyboard event

    hi,

    I didn't get how this process is being taking place...

    Can you provide me a sample program that takes a keybord input on the window like up arrow press and message "Up arrow Pressed"...

    Plz do consider...

  4. #4

    Default Re: Capture a keyboard event

    For Qt 3.3.x:
    There is great example in the Documentation. U can simply use QWidget::keyPressEvent()

    Qt Code:
    1. void PictureDisplay::keyPressEvent( QKeyEvent *k )
    2. {
    3. switch ( tolower(k->ascii()) ) {
    4. case 'r': // reload
    5. pict->load( name );
    6. update();
    7. break;
    8. case 'q': // quit
    9. QApplication::exit();
    10. break;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Here: http://doc.trolltech.com/3.3/picture-example.html#x127
    I am sailing with the cane...

  5. #5
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Capture a keyboard event

    hi,

    I gone thru the codes you given and understood the concept...
    Thank you...

    Now i experienced a problem that all keys except arrow keys are detected by QKeyEvent...
    Can you tell why arrow keys are not detected...

    Or whether the arrow key press are not among QKeyEvent...

    Help me plz...


    Mahesh


    Quote Originally Posted by dec0ding
    For Qt 3.3.x:
    There is great example in the Documentation. U can simply use QWidget::keyPressEvent()

    Qt Code:
    1. void PictureDisplay::keyPressEvent( QKeyEvent *k )
    2. {
    3. switch ( tolower(k->ascii()) ) {
    4. case 'r': // reload
    5. pict->load( name );
    6. update();
    7. break;
    8. case 'q': // quit
    9. QApplication::exit();
    10. break;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Here: http://doc.trolltech.com/3.3/picture-example.html#x127

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Capture a keyboard event

    Can you show your code that is dealing with the key events?
    If you are using ascii() for these keys it wont work since they don't have an ascii() representation, you should use the Qt::Key value.

  7. #7
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Capture a keyboard event

    Quote Originally Posted by high_flyer
    Can you show your code that is dealing with the key events?
    If you are using ascii() for these keys it wont work since they don't have an ascii() representation, you should use the Qt::Key value.
    I could receive the arrow press event but on enter press nothing is happening except a repeating of
    other events (defined in the code).....

    key_press is my global variable

    void MyWidget::keyPressEvent(QKeyEvent *event)
    {
    if(event->key() == Qt::Key_Up) {
    key_press = -1;
    }am able to
    else if(event->key() == Qt::Key_Down) {
    key_press = 1;
    }am able to
    else if(event->key() == Qt::Key_Enter) {
    key_press = 3; // But this part of the code is not reached on enter key press
    }
    else {
    QWidget::keyPressEvent(event);
    }

    // other events....

    }
    Last edited by mahe2310; 16th February 2006 at 09:19. Reason: fail to include few ogf the statements ...

  8. #8
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    1
    Thanked 4 Times in 1 Post

    Default Re: Capture a keyboard event

    I had a similiar problem a day ago, i posted my solution in the end how to catch Tab, enter and space and so on.

    http://www.qtcentre.org/forum/showthread.php?t=670

  9. #9
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Capture a keyboard event

    Quote Originally Posted by lumber44
    I had a similiar problem a day ago, i posted my solution in the end how to catch Tab, enter and space and so on.

    http://www.qtcentre.org/forum/showthread.php?t=670

    thanks lumber... That is a good one...

    Mahe2310

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 11:10
  2. Event propagation direction
    By spraff in forum Qt Programming
    Replies: 0
    Last Post: 6th December 2008, 21:03
  3. how to manipulate the keyboard event?
    By Ricardo_arg in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 07:49
  4. On Screen Keyboard Problem
    By cutie.monkey in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2008, 13:28
  5. Qt event queue overloading?
    By gct in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 18:39

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.