Disable control keyboard button



  • Hello everyone.

    I have a little question about to disable the control button in keyboard.

    I have a

    *keyPressEvent(QKeyEvent event)

    function and I want to disable de control button. I tried with the

    event->ignore()

    sentence but it doesn't works.

    How I can do this?

    Thank you very much!


  • Lifetime Qt Champion

    Hi,

    IIRC, CTRL is a modifier.

    What exactly do you want to achieve ?


  • Moderators

    @ivanicy Are you saying you want to stop the control button from being used anywhere in the OS? Or just in your application?

    You won't be able to disable it for the OS using Qt, you will need to go native for that.

    It will be mildly difficult in your application as well. What is your reason for wanting to disable control? It is a very used modifier key in all OSes and really should not be disabled. I can guarantee I wouldn't use your app if it disabled my ctrl key. ;)



  • @ivanicy

    Use nativeEventFilter() if you only need to use it in your application.
    The following example is intended for use with Windows. It can be used similarly on other operating systems.

    [...]
    #include <QAbstractNativeEventFilter>
    #include <QDebug>
    #include <windows.h>
    
    class MyWindowsEventFilter : public QAbstractNativeEventFilter
    {
    public:
        virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE
        {
            if (eventType == "windows_generic_MSG") {
                MSG* ev = static_cast<MSG *>(message);
                if (ev->message == WM_KEYDOWN && GetAsyncKeyState(VK_CONTROL)) {
                    qDebug() << "Captured CONTROL+" << ev->wParam;
                    return true;
                }
            }
            return false;
        }
    };
    [...]
    
    // in main.cpp
    [...]
    QApplication a(argc, argv);
    a.installNativeEventFilter(new MyWindowsEventFilter);
    [...]
    

    If you want to capture only the CONTROL key, modify it as follows.

    [...]
    if (ev->message == WM_KEYDOWN && GetAsyncKeyState(VK_CONTROL)) {
        if (ev->wParam == VK_CONTROL) {
            qDebug() << "Captured CONTROL key";
            return true;
        }
    }
    [...]
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.