<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qevent.cpp --> <title>QKeyEvent Class | Qt GUI 5.9</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.9</td><td ><a href="qtgui-index.html">Qt GUI</a></td><td ><a href="qtgui-module.html">C++ Classes</a></td><td >QKeyEvent</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#public-functions">Public Functions</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QKeyEvent Class</h1> <!-- $$$QKeyEvent-brief --> <p>The <a href="qkeyevent.html">QKeyEvent</a> class describes a key event. <a href="#details">More...</a></p> <!-- @@@QKeyEvent --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign"> <span class="preprocessor">#include <QKeyEvent></span> </td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += gui</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherits:</td><td class="memItemRight bottomAlign"> <a href="qinputevent.html">QInputEvent</a></td></tr></table></div><ul> <li><a href="qkeyevent-members.html">List of all members, including inherited members</a></li> </ul> <a name="public-functions"></a> <h2 id="public-functions">Public Functions</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#QKeyEvent">QKeyEvent</a></b>(Type <i>type</i>, int <i>key</i>, Qt::KeyboardModifiers <i>modifiers</i>, const QString &<i>text</i> = QString(), bool <i>autorep</i> = false, ushort <i>count</i> = 1)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#QKeyEvent-1">QKeyEvent</a></b>(Type <i>type</i>, int <i>key</i>, Qt::KeyboardModifiers <i>modifiers</i>, quint32 <i>nativeScanCode</i>, quint32 <i>nativeVirtualKey</i>, quint32 <i>nativeModifiers</i>, const QString &<i>text</i> = QString(), bool <i>autorep</i> = false, ushort <i>count</i> = 1)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#count">count</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#isAutoRepeat">isAutoRepeat</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#key">key</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#matches">matches</a></b>(QKeySequence::StandardKey <i>key</i>) const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> Qt::KeyboardModifiers </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#modifiers">modifiers</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> quint32 </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#nativeModifiers">nativeModifiers</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> quint32 </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#nativeScanCode">nativeScanCode</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> quint32 </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#nativeVirtualKey">nativeVirtualKey</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qkeyevent.html#text">text</a></b>() const</td></tr> </table></div> <ul> <li class="fn">2 public functions inherited from <a href="qinputevent.html#public-functions">QInputEvent</a></li> <li class="fn">6 public functions inherited from <a href="../qtcore/qevent.html#public-functions">QEvent</a></li> </ul> <h3>Additional Inherited Members</h3> <ul> <li class="fn">1 property inherited from <a href="../qtcore/qevent.html#properties">QEvent</a></li> <li class="fn">1 static public member inherited from <a href="../qtcore/qevent.html#static-public-members">QEvent</a></li> <li class="fn">2 protected variables inherited from <a href="../qtcore/qevent.html#protected-variables">QEvent</a></li> </ul> <a name="details"></a> <!-- $$$QKeyEvent-description --> <div class="descr"> <h2 id="details">Detailed Description</h2> <p>The <a href="qkeyevent.html">QKeyEvent</a> class describes a key event.</p> <p>Key events are sent to the widget with keyboard input focus when keys are pressed or released.</p> <p>A key event contains a special accept flag that indicates whether the receiver will handle the key event. This flag is set by default for <a href="../qtcore/qevent.html#Type-enum">QEvent::KeyPress</a> and <a href="../qtcore/qevent.html#Type-enum">QEvent::KeyRelease</a>, so there is no need to call <a href="../qtcore/qevent.html#accept">accept</a>() when acting on a key event. For <a href="../qtcore/qevent.html#Type-enum">QEvent::ShortcutOverride</a> the receiver needs to explicitly accept the event to trigger the override. Calling <a href="../qtcore/qevent.html#ignore">ignore</a>() on a key event will propagate it to the parent widget. The event is propagated up the parent widget chain until a widget accepts it or an event filter consumes it.</p> <p>The <a href="../qtwidgets/qwidget.html#enabled-prop">QWidget::setEnabled</a>() function can be used to enable or disable mouse and keyboard events for a widget.</p> <p>The event handlers <a href="../qtwidgets/qwidget.html#keyPressEvent">QWidget::keyPressEvent</a>(), <a href="../qtwidgets/qwidget.html#keyReleaseEvent">QWidget::keyReleaseEvent</a>(), <a href="../qtwidgets/qgraphicsitem.html#keyPressEvent">QGraphicsItem::keyPressEvent</a>() and <a href="../qtwidgets/qgraphicsitem.html#keyReleaseEvent">QGraphicsItem::keyReleaseEvent</a>() receive key events.</p> </div> <p><b>See also </b><a href="qfocusevent.html">QFocusEvent</a> and <a href="../qtwidgets/qwidget.html#grabKeyboard">QWidget::grabKeyboard</a>().</p> <!-- @@@QKeyEvent --> <div class="func"> <h2>Member Function Documentation</h2> <!-- $$$QKeyEvent[overload1]$$$QKeyEventTypeintQt::KeyboardModifiersconstQString&boolushort --> <h3 class="fn" id="QKeyEvent"><a name="QKeyEvent"></a>QKeyEvent::<span class="name">QKeyEvent</span>(<span class="type"><a href="../qtcore/qevent.html#Type-enum">Type</a></span> <i>type</i>, <span class="type">int</span> <i>key</i>, <span class="type"><a href="../qtcore/qt.html#KeyboardModifier-enum">Qt::KeyboardModifiers</a></span> <i>modifiers</i>, const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &<i>text</i> = QString(), <span class="type">bool</span> <i>autorep</i> = false, <span class="type"><a href="../qtcore/qtglobal.html#ushort-typedef">ushort</a></span> <i>count</i> = 1)</h3> <p>Constructs a key event object.</p> <p>The <i>type</i> parameter must be <a href="../qtcore/qevent.html#Type-enum">QEvent::KeyPress</a>, <a href="../qtcore/qevent.html#Type-enum">QEvent::KeyRelease</a>, or <a href="../qtcore/qevent.html#Type-enum">QEvent::ShortcutOverride</a>.</p> <p>Int <i>key</i> is the code for the <a href="../qtcore/qt.html#Key-enum">Qt::Key</a> that the event loop should listen for. If <i>key</i> is 0, the event is not a result of a known key; for example, it may be the result of a compose sequence or keyboard macro. The <i>modifiers</i> holds the keyboard modifiers, and the given <i>text</i> is the Unicode text that the key generated. If <i>autorep</i> is true, <a href="qkeyevent.html#isAutoRepeat">isAutoRepeat</a>() will be true. <i>count</i> is the number of keys involved in the event.</p> <!-- @@@QKeyEvent --> <!-- $$$QKeyEvent$$$QKeyEventTypeintQt::KeyboardModifiersquint32quint32quint32constQString&boolushort --> <h3 class="fn" id="QKeyEvent-1"><a name="QKeyEvent-1"></a>QKeyEvent::<span class="name">QKeyEvent</span>(<span class="type"><a href="../qtcore/qevent.html#Type-enum">Type</a></span> <i>type</i>, <span class="type">int</span> <i>key</i>, <span class="type"><a href="../qtcore/qt.html#KeyboardModifier-enum">Qt::KeyboardModifiers</a></span> <i>modifiers</i>, <span class="type"><a href="../qtcore/qtglobal.html#quint32-typedef">quint32</a></span> <i>nativeScanCode</i>, <span class="type"><a href="../qtcore/qtglobal.html#quint32-typedef">quint32</a></span> <i>nativeVirtualKey</i>, <span class="type"><a href="../qtcore/qtglobal.html#quint32-typedef">quint32</a></span> <i>nativeModifiers</i>, const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &<i>text</i> = QString(), <span class="type">bool</span> <i>autorep</i> = false, <span class="type"><a href="../qtcore/qtglobal.html#ushort-typedef">ushort</a></span> <i>count</i> = 1)</h3> <p>Constructs a key event object.</p> <p>The <i>type</i> parameter must be <a href="../qtcore/qevent.html#Type-enum">QEvent::KeyPress</a>, <a href="../qtcore/qevent.html#Type-enum">QEvent::KeyRelease</a>, or <a href="../qtcore/qevent.html#Type-enum">QEvent::ShortcutOverride</a>.</p> <p>Int <i>key</i> is the code for the <a href="../qtcore/qt.html#Key-enum">Qt::Key</a> that the event loop should listen for. If <i>key</i> is 0, the event is not a result of a known key; for example, it may be the result of a compose sequence or keyboard macro. The <i>modifiers</i> holds the keyboard modifiers, and the given <i>text</i> is the Unicode text that the key generated. If <i>autorep</i> is true, <a href="qkeyevent.html#isAutoRepeat">isAutoRepeat</a>() will be true. <i>count</i> is the number of keys involved in the event.</p> <p>In addition to the normal key event data, also contains <i>nativeScanCode</i>, <i>nativeVirtualKey</i> and <i>nativeModifiers</i>. This extra data is used by the shortcut system, to determine which shortcuts to trigger.</p> <!-- @@@QKeyEvent --> <!-- $$$count[overload1]$$$count --> <h3 class="fn" id="count"><a name="count"></a><span class="type">int</span> QKeyEvent::<span class="name">count</span>() const</h3> <p>Returns the number of keys involved in this event. If <a href="qkeyevent.html#text">text</a>() is not empty, this is simply the length of the string.</p> <p><b>See also </b><a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_KeyCompression</a>.</p> <!-- @@@count --> <!-- $$$isAutoRepeat[overload1]$$$isAutoRepeat --> <h3 class="fn" id="isAutoRepeat"><a name="isAutoRepeat"></a><span class="type">bool</span> QKeyEvent::<span class="name">isAutoRepeat</span>() const</h3> <p>Returns <code>true</code> if this event comes from an auto-repeating key; returns <code>false</code> if it comes from an initial key press.</p> <p>Note that if the event is a multiple-key compressed event that is partly due to auto-repeat, this function could return either true or false indeterminately.</p> <!-- @@@isAutoRepeat --> <!-- $$$key[overload1]$$$key --> <h3 class="fn" id="key"><a name="key"></a><span class="type">int</span> QKeyEvent::<span class="name">key</span>() const</h3> <p>Returns the code of the key that was pressed or released.</p> <p>See <a href="../qtcore/qt.html#Key-enum">Qt::Key</a> for the list of keyboard codes. These codes are independent of the underlying window system. Note that this function does not distinguish between capital and non-capital letters, use the <a href="qkeyevent.html#text">text</a>() function (returning the Unicode text the key generated) for this purpose.</p> <p>A value of either 0 or <a href="../qtcore/qt.html#Key-enum">Qt::Key_unknown</a> means that the event is not the result of a known key; for example, it may be the result of a compose sequence, a keyboard macro, or due to key event compression.</p> <p><b>See also </b><a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_KeyCompression</a>.</p> <!-- @@@key --> <!-- $$$matches[overload1]$$$matchesQKeySequence::StandardKey --> <h3 class="fn" id="matches"><a name="matches"></a><span class="type">bool</span> QKeyEvent::<span class="name">matches</span>(<span class="type"><a href="qkeysequence.html#StandardKey-enum">QKeySequence::StandardKey</a></span> <i>key</i>) const</h3> <p>Returns <code>true</code> if the key event matches the given standard <i>key</i>; otherwise returns <code>false</code>.</p> <p>This function was introduced in Qt 4.2.</p> <!-- @@@matches --> <!-- $$$modifiers[overload1]$$$modifiers --> <h3 class="fn" id="modifiers"><a name="modifiers"></a><span class="type"><a href="../qtcore/qt.html#KeyboardModifier-enum">Qt::KeyboardModifiers</a></span> QKeyEvent::<span class="name">modifiers</span>() const</h3> <p>Returns the keyboard modifier flags that existed immediately after the event occurred.</p> <p><b>Warning:</b> This function cannot always be trusted. The user can confuse it by pressing both <b>Shift</b> keys simultaneously and releasing one of them, for example.</p> <p><b>See also </b><a href="qguiapplication.html#keyboardModifiers">QGuiApplication::keyboardModifiers</a>().</p> <!-- @@@modifiers --> <!-- $$$nativeModifiers[overload1]$$$nativeModifiers --> <h3 class="fn" id="nativeModifiers"><a name="nativeModifiers"></a><span class="type"><a href="../qtcore/qtglobal.html#quint32-typedef">quint32</a></span> QKeyEvent::<span class="name">nativeModifiers</span>() const</h3> <p>Returns the native modifiers of a key event. If the key event does not contain this data 0 is returned.</p> <p>Note: The native modifiers may be 0, even if the key event contains extended information.</p> <p>This function was introduced in Qt 4.2.</p> <!-- @@@nativeModifiers --> <!-- $$$nativeScanCode[overload1]$$$nativeScanCode --> <h3 class="fn" id="nativeScanCode"><a name="nativeScanCode"></a><span class="type"><a href="../qtcore/qtglobal.html#quint32-typedef">quint32</a></span> QKeyEvent::<span class="name">nativeScanCode</span>() const</h3> <p>Returns the native scan code of the key event. If the key event does not contain this data 0 is returned.</p> <p>Note: The native scan code may be 0, even if the key event contains extended information.</p> <p>Note: On Mac OS/X, this function is not useful, because there is no way to get the scan code from Carbon or Cocoa. The function always returns 1 (or 0 in the case explained above).</p> <p>This function was introduced in Qt 4.2.</p> <!-- @@@nativeScanCode --> <!-- $$$nativeVirtualKey[overload1]$$$nativeVirtualKey --> <h3 class="fn" id="nativeVirtualKey"><a name="nativeVirtualKey"></a><span class="type"><a href="../qtcore/qtglobal.html#quint32-typedef">quint32</a></span> QKeyEvent::<span class="name">nativeVirtualKey</span>() const</h3> <p>Returns the native virtual key, or key sym of the key event. If the key event does not contain this data 0 is returned.</p> <p>Note: The native virtual key may be 0, even if the key event contains extended information.</p> <p>This function was introduced in Qt 4.2.</p> <!-- @@@nativeVirtualKey --> <!-- $$$text[overload1]$$$text --> <h3 class="fn" id="text"><a name="text"></a><span class="type"><a href="../qtcore/qstring.html">QString</a></span> QKeyEvent::<span class="name">text</span>() const</h3> <p>Returns the Unicode text that this key generated.</p> <p>Return values when modifier keys such as Shift, Control, Alt, and Meta are pressed differ among platforms and could return an empty string.</p> <p><b>Note: </b><a href="qkeyevent.html#key">key</a>() will always return a valid value, independent of modifier keys.</p><p><b>See also </b><a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_KeyCompression</a>.</p> <!-- @@@text --> </div> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>