English 中文(简体)
• 如何正确探测用QKeyEvent的金属钥匙?
原标题:How to correctly detect the meta key with a QKeyEvent?
  • 时间:2012-05-14 11:32:50
  •  标签:
  • qt

我试图用Qt执行一个短击手,使用户能够洗手。 我希望,它具有互动性,这样一米会像目前所打的那样展现出捷径,而不仅仅是在完成的时候。 我认为,我不是远未解决问题的,但我仍有一些问题与元钥匙(Windows钥匙)有关。

第一种情况似乎会发现,如果使用假肢者功能的话语句如下,但不是最后的。

void ShortcutInputWidget::handleKeyEvent(QKeyEvent *event)
{
    int keyDisplayed = event->key();
    Qt::Key keys = static_cast<Qt::Key>(keyDisplayed);

    // if the key pressed is only a modifier, we reset the key
    if(keys == Qt::Key_Control ||
        keys == Qt::Key_Shift ||
        keys == Qt::Key_Alt ||
        keys == Qt::Key_Meta)
    { 
        qDebug() << "Single press of special key: Ctrl, Shift, Alt or Meta";
        if (keys == Qt::Key_Meta)
            qDebug() << "meta: " << QKeySequence(Qt::META);
        keyDisplayed = 0;
    }

    // handle modifiers
    Qt::KeyboardModifiers modifiers = event->modifiers();
    QString text = event->text();
    qDebug() << text;
    if (modifiers & Qt::ShiftModifier){
        keyDisplayed |= Qt::SHIFT;
        qDebug() << "shift modifier detected";
    }
    if (modifiers & Qt::ControlModifier){
        keyDisplayed |= Qt::CTRL;
        qDebug() << "control modifier detected";
    }
    if (modifiers & Qt::MetaModifier){
        keyDisplayed |= Qt::META;
        qDebug() << "meta detected";
    }
    if (modifiers & Qt::AltModifier){
        keyDisplayed |= Qt::ALT;
        qDebug() << "alt modifier detected";
    }

    qDebug() << QKeySequence(keyDisplayed);
    setText(QKeySequence(keyDisplayed));
    event->accept();
}

如果我只打了元钥匙,则产出为<代码>meta:QKeySequence(“Meta+”),但没有发现的<代码>meta>。 但是,如果我打上了“ctrl + meta”号,则这两张话都探测到了元钥匙。

我做了一些错误?

EDIT:我只想提及使用Windows 7的Im,我有一个EN-GB键盘

最佳回答

视窗钥匙似乎不是modifier。 贵方对贵方所获得价值进行删减或打印时

 Qt::KeyboardModifiers modifiers = event->modifiers();

你们在把窗户关起来时获得空洞的旗帜,但对于你所处理的其他关键人物来说,没有空洞的旗帜。 我认为这是有意义的,因为根据我的经验, 窗口钥匙从未使用逐用户应用程序<>。

现在,Mac Key公司在MacOS公司违约后是变造者,而这一钥匙的窗户在使用Mac计算机的窗户时被分配到。 这确实是trick笑的,因为它在管理第X号业务时可以成为有效的变压器,在使用Windows时不能有效。

The only advice I can give is to think again about what you want to achieve, and then test on Mac, Windows, and Linux separately. Sometimes it is necessary to ensure portability.

问题回答

Well I run the same sample On Linux[Qt creator]. It worked perfectly, both messages were displayed. Might be some OS related issue in your case. The key I pressed was windows key which it detected as meta key.

Single press of special key: Ctrl, Shift, Alt or Meta 
meta:  QKeySequence("Meta+") 
"" 
meta detected 
QKeySequence("Meta+")  




相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...