Keyboard events in Qt, and settings for focus (more details)

Source: Internet
Author: User
Tags key loop



QT keyboard events are part of the QT event System, so all rules in the event system are valid for keystroke events. The following concerns are part of the key-specific section:


Focus


A qwidget that has focus can accept keyboard events. The window with the input focus is the active window or the active Window child window or child window, and so on.



The focus moves in several ways:


    • Press Tab or Shift+tab
      • Note: Text compiler (usually requires tab), or WebView (Requires tab to move hyperlink focus), etc.

      • In Qt, the place where you need to enter the TAB can be replaced with Ctrl+tab or Ctrl+shift+tab.
    • Click on a Qwidget
      • Recommendation: Enable this feature only for widgets that accept text input
    • Press the keyboard shortcut key
      • Qlabel::setbuddy (), Qgroupbox, and Qtabbar support
    • Use the mouse wheel
    • User Move focus
      • The program will determine which of the widgets that are set by the focus gets the focus


Note: If a widget is already grabkeyboard, all keyboard events will be sent to the widget instead of the widget that gets the focus


Focuspolicy


The way a qwidget gets focus is controlled by Focuspolicy


Qt::tabfocusGet focus by Tab keyQt::clickfocusGet focus by clickingQt::strongfocusThe focus can be obtained from both of the above methodsQt::nofocusThefocus cannot be obtained in either of the two ways (the default), and the SetFocus still gives it the focusKeyPress and Keyrelease


First of all, if the widget gets the focus, it's generally set to Focuspolicy.



Then, to respond to the keystrokes, we just need to overload them directly:


    • Keypressevent
    • Keyreleaseevent


Attention:


    • For events that we do not handle, call the corresponding event handler for the parent class.
    • If the widget does not currently have focus, consider event forwarding: If its child widget has the focus, keyboard events that the widget has not handled will be forwarded over.
    • Sometimes the input focus is not in any window. This happens when all programs are minimized. At this point, Windows will continue to send keyboard messages to the active window, but these messages have different forms from the keyboard messages sent to the non-minimized active window.
Qkeyevent


Under Windows, there are 8 messages related to keyboard events:


    • For key combinations that produce a display character, Windows not only sends a keystroke message to the program, but also sends a character message
    • Some keys do not produce characters, including the SHIFT key, function keys, cursor movement keys, and special character keys such as insert and delete. For these keys, Windows produces only key messages.


These messages are only reflected in Qkeyevent in Qt.


    • Pairs of characters that can be obtained by Qkeyevent::text ()
    • Other keys, Qkeyevent::key () get a key value





Actual program:



Public:void keypressevent (qkeyevent *event);



A widget is required to set Labelcomment->setfocuspolicy (Qt::strongfocus) before the corresponding keyboard event.



void Camerashow::keypressevent (Qkeyevent *event)
{



if (Event->key () ==qt::key_q)
{
Pause_flag ^= 1;
}



}






There is also information: (Qtextbrowser's URL switch)



Http://hi.baidu.com/%D7%D4%D3%EF%B5%C4%C2%E6%CD%D5/blog/item/2758cf13d83f945df819b806.html






In Qt, you can use void Qwidget::keypressevent (Qkeyevent * k) for keyboard responses, such as:



void Form1::keypressevent (Qkeyevent *k)
{
if (k->key () = = key_a)
{
This->focusnextprevchild (FALSE);//press A when focus switches to the first part
}
else if (k->key () = = Key_d)
{
This->focusnextprevchild (TRUE);//press D to switch focus to the next part
}
else if (k->key () = = Key_w)
{
if (k->state () = = Qt::shiftbutton)
{
This->resize (100,100);//Change the window size when shift+w is pressed
}
}
}



However, there are some special keys such as TAB key, if implemented in Keypressevent is not successful, because the Default tab event (toggle Focus) is captured first, the default tab and Shift+tab event is defined in Qwidget.h, the code is:


Case Qevent::keypress: {
Qkeyevent *k = (qkeyevent *) e;
bool res = FALSE;
if (k->key () = = Key_backtab | |
(K->key () = = Key_tab &&
(K->state () & Shiftbutton)) ) {
Qfocusevent::setreason (Qfocusevent::tab);
res = Focusnextprevchild (FALSE);
Qfocusevent::resetreason ();
} else if (k->key () = = Key_tab) {
Qfocusevent::setreason (Qfocusevent::tab);
res = Focusnextprevchild (TRUE);
Qfocusevent::resetreason ();
}
}



So we have to implement our own tab event before. The implementation code is as follows:


bool MyWidget::event(QEvent *event)

{

if (event->type() == QEvent::KeyPress) {

QKeyEvent *ke = static_cast(event);

if (ke->key() == Qt::Key_Tab) {

// special tab handling here

return true;

}

} else if (event->type() == MyCustomEventType) {

MyCustomEvent *myEvent = static_cast(event);

// custom event handling here

return true;

}



return QWidget::event(event);

}

URL switching in Qtextbrowser
void Almtextview::keypressevent (qkeyevent* e)
{
Amdebug ("Almtextview special Key event/n");
Qscrollbar *SBV = Verticalscrollbar ();
Switch (E->key ()) {
Case Key_right:
Case Key_down:
if (!selectnextprevhref (TRUE))
{
Scroll the screen down by one page
if (sbv->value () = = Sbv->maxvalue ())
{
printf ("asdasdasdasdasdasda/n");
This->focusnextprevchild (TRUE);
}
Sbv->setvalue (Sbv->value () + (Sbv->pagestep () >> 1));
Selectnextprevhref (TRUE);
}
E->accept ();
Return
Case Key_left:
Case KEY_UP:
if (!selectnextprevhref (FALSE)) {
Scroll the screen up by one page
if (sbv->value () = = 0)
{
printf ("1234567890/n");
This->focusnextprevchild (FALSE);
}
Sbv->setvalue (Sbv->value ()-(Sbv->pagestep () >> 1));
Selectnextprevhref (FALSE);
}
E->accept ();
Return
}
Qtextview::keypressevent (e);
}





Qkeyevent class Reference
The Qkeyevent class is used to describe the QT events generated by keyboard keys
#include <QKeyEvent>
Inherit from Qinpueevent
Public functions:



Qkeyevent (type type, int key, qt::keyboardmodifiers modifiers, const QString & text = QString (), bool Autorep = Fals E, ushort count = 1)



int count () const
BOOL Isautorepeat () const
int key () const
BOOL Matches (Qkeysequence::standardkey key) const
Qt::keyboardmodifiers modifiers () const
Quint32 nativemodifiers () const
Quint32 Nativescancode () const
Quint32 Nativevirtualkey () const
QString text () const
Related non-member functions:
BOOL operator== (Qkeyevent * e, Qkeysequence::standardkey key)
BOOL operator== (Qkeysequence::standardkey key, Qkeyevent * e)
Detail Description:
The Qkeyevent class is used to describe the QT events produced by keyboard keys, and the QT keyboard events that occur when a window has input focus, via the keyboard key (TAP/release)
Can be passed to a window with input focus for processing.
The keyboard QT event contains a number of specified receive flags to indicate whether the event is handled by the recipient of the QT event, if the window does not handle the keyboard (TAP/release)
can be ignored by calling the Ignore () function. The keyboard QT event has transitivity, which is transmitted to the parent window and to the top-level window, the linked list, the first layer of the transfer,
Until a window calls accept (), or filters until the event is destroyed. Keyboard QT events are always ignored by default for multimedia,
If your window is to handle the event, you can receive it by calling the Accept () function.


You can set a window to enable/disable receiving mouse or keyboard events by calling the Qwidget::setenable () function.
QT Event handle function qwidget::keypressevent (), Qwidget::keyreleaseevent (), Qgraphicsitem::keypressevent ()
, Qgraphicsitem::keyreleaseevent () can be used to receive keyboard events.


member function Description:


Qkeyevent::qkeyevent (type type, int key, qt::keyboardmodifiers modifiers, const QString & text = QString (), BOOL AUT Orep = False, ushort count = 1)


Initializes a KeyEvent object.
The parameter type must be qevent::keypress, qevent::keyrelease, or qevent::shortcutoverride one of them.
The parameter key is used to listen for the Qt::key loop event, and if key is 0 it causes the keyboard event to be an undefined key, for example, the possible result of the keyboard event is
Continuous push-button operation or pre-defined shortcut keys.
The parameter modifiers is used to indicate the change of keyboard key value, and the corresponding Unicode encoding information of the key is given.
If the parameter autorep is true, the function isautorepeat () function returns True
The parameter count is used to calculate the number of keystrokes that are involved in the keyboard event.


int Qkeyevent::count () const
Returns the number of keystrokes involved in the keyboard QT event, and if the function text () returns not NULL, this function simply computes the length of the text string.


BOOL Qkeyevent::isautorepeat () const
Returns true if the keyboard Qt event is caused by an auto-repeating key for some columns, otherwise false
Note that the function is not sure whether to return true or False if some of the key combinations that result in multiple overlays are generated as part of the auto-looping form.


int Qkeyevent::key () const
Returns the encoded value corresponding to the pressed or released button. View the Qt::key keyboard-encoded list, which is dependent on the underlying operating system.
The function is not case-sensitive, to know whether the keys for the returned key encoding are uppercase or lowercase (such as a and a) can be obtained by invoking the text () function.
The numbers 0 and qt::key_unknown represent the result of a Well_known key that causes the QT keyboard event to occur, for example, a series of complex input that causes the keyboard event, either a shortcut key or a compressed key.

BOOL Qkeyevent::matches (Qkeysequence::standardkey key) const
Returns true if the key encoding value that causes the keyboard QT event matches the standard key value given by the parameter key, or false otherwise.
This function is described in QT4.2. The function can iterate over which key is currently pressed.


Qt::keyboardmodifiers qkeyevent::modifiers () const
The keyboard QT event is generated and returned directly to the hotkey that caused the event. Tip: The function does not always return very precise results when the user presses the shift+ key combination at the same time or
When you release one of these keys, the function may return a result that confuses the user.


Quint32 qkeyevent::nativemodifiers () const
Returns the hotkey encoded value that caused the keyboard QT event, and returns the number 0 if the event does not contain the corresponding hotkey.
Note: Even if the keyboard event contains extended information, the hotkey may be 0.
This function is described in QT4.2.


Quint32 Qkeyevent::nativescancode () const
Returns the scan code that caused the QT event for the keyboard, and returns the number 0 if the Dare event does not contain the corresponding scan code.
Note: Even if the keyboard event contains extended information, the scan code may be 0.
Note: This function is invalid on Mac os/x system because there is no way to get the scan code from cocoa or carbon, the function usually returns 1, or 0 when the extension information is included.
This function is described in QT4.2


Quint32 Qkeyevent::nativevirtualkey () const
Returns the virtual key or character code that causes the keyboard QT event to return 0 if the keyboard event does not contain the encoding for the key.
Note: Even if the keyboard event contains extended information, the virtual key may be 0.
This function is described in QT4.2


QString Qkeyevent::text () const
Returns the Unicode character encoding information of the resulting keyboard event, when the cause of the keyboard event is caused by a hotkey such as SHIFT, Control, ALT, etc., or released,
The character returned by the function can be a null value. In this case, the function key () can return a valid value.


Related non-member functions:
BOOL operator== (Qkeyevent * e, Qkeysequence::standardkey key)
Returns true if the parameter E and the parameter key type match and are equal
Equivalent to function e->matches (key).


BOOL operator== (Qkeysequence::standardkey key, Qkeyevent * e)
Returns true if the parameter E and the parameter key type match and are equal
Equivalent to function e->matches (key).





http://blog.csdn.NET/zzk197/article/details/7383715



http://blog.csdn.Net/free_program_1314/article/details/7681456






http://blog.csdn.net/huyisu/article/details/30455563



Keyboard events in Qt, and settings for focus (more details)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.