Low level keyboard hook on windows ce



  • Hi all,

    I have tried and looked everywhere however, I cannot seem to find a way to low level keyboard hook under windows ce. All the examples I have found use SetWindowsHookEx which is apparently unsupported under windows ce. The examples for windows ce I have found use loadlibrary to load coredll.dll and call the SetWindowsHookEx function that way.

    As far as I know there is QLibrary which can perform the same function unfortunately I am unable to wrap my head around combining these concepts.

    Does anyone know of a way to low level keyboard hook on windows ce and show me how I can do it? Example code would be fantastic or even an RTFM this old head of mine can understand.

    Thanks for your time.



  • I have been doing some research on this and this is what I have managed to come up with.

    In the header I have put
    @
    #include <QtGui/QMainWindow>
    #include <QLibrary>

    typedef struct tagKBDLLHOOKSTRUCT {
    DWORD vkCode;
    DWORD scanCode;
    DWORD flags;
    DWORD time;
    ULONG_PTR dwExtraInfo;
    } KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT, *LPKBDLLHOOKSTRUCT;

    class testClass : public QMainWindow
    {
    Q_OBJECT
    private:
    friend LRESULT CALLBACK LowLevelKeyboardProc(int code, WPARAM wparam, LPARAM lparam);
    }@

    in the implementation file I have.

    @
    #include "testClass.h"

    LRESULT CALLBACK LowLevelKeyboardProc(int code, WPARAM wparam, LPARAM lparam)
    {
    int HC_ACTION = 0;

    typedef int (cNextHookEx)(int, int, WPARAM, LPARAM);
    cNextHookEx CallNextHookEx = (cNextHookEx) QLibrary::resolve("coredll","CallNextHookEx");
    KBDLLHOOKSTRUCT
    kllhs = reinterpret_cast<KBDLLHOOKSTRUCT*>(lparam);
    if (code == HC_ACTION)
    {
    if (wparam == WM_KEYDOWN && (kllhs->vkCode == VK_F3 || kllhs->vkCode == VK_F4) &&
    (GetAsyncKeyState(VK_MENU) < 0 && GetAsyncKeyState(VK_CONTROL) < 0))
    {
    mobiMeter* w = dynamic_cast<mobiMeter*>(qApp->activeWindow());
    if (NULL != w)
    {
    qDebug() << " this should not be blocking!";
    return 1;
    }
    }
    }

    return CallNextHookEx(0, code, wparam, lparam);
    

    }

    testClass::testClass(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    int WH_KEYBOARD_LL = 20;
    ui.setupUi(this);
    typedef int (*sWindowHookEx)(int, int, int, int);
    sWindowHookEx SetWindowsHookEx = (sWindowHookEx) QLibrary::resolve("coredll","SetWindowsHookEx");
    SetWindowsHookEx(WH_KEYBOARD_LL,
    (int)LowLevelKeyboardProc,
    NULL,
    0);

    }@

    It compiles fine yet when I try to run it, it crashes. Now I don't know what to do :( Any ideas?


Log in to reply
 

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