capture QKeySequence from QKeyEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
if (event->type() == QEvent::KeyPress){ 
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); 
 
int keyInt = keyEvent->key(); 
Qt::Key key = static_cast<Qt::Key>(keyInt); 
if(key == Qt::Key_unknown){ 
    qDebug() << "Unknown key from a macro probably"; 
    return; 
} 
// the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta. 
if(key == Qt::Key_Control || 
    key == Qt::Key_Shift || 
    key == Qt::Key_Alt || 
    key == Qt::Key_Meta)
{ 
    qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta"; 
    qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText); 
    return; 
} 
 
// check for a combination of user clicks 
Qt::KeyboardModifiers modifiers = keyEvent->modifiers(); 
QString keyText = keyEvent->text(); 
// if the keyText is empty than it's a special key like F1, F5, ... 
qDebug() << "Pressed Key:" << keyText; 
 
QList<Qt::Key> modifiersList; 
if(modifiers & Qt::ShiftModifier) 
    keyInt += Qt::SHIFT; 
if(modifiers & Qt::ControlModifier) 
    keyInt += Qt::CTRL; 
if(modifiers & Qt::AltModifier) 
    keyInt += Qt::ALT; 
if(modifiers & Qt::MetaModifier) 
    keyInt += Qt::META; 
 
qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText); 
}
Category(s): Qt

Leave a Reply

Your email address will not be published. Required fields are marked *

*

 

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">