运行C ++事件循环,不使用QT [英] Running a C++ Event Loop WITHOUT using QT

查看:536
本文介绍了运行C ++事件循环,不使用QT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图在c ++中开发一个后台Windows应用程序来捕获系统级的击键和鼠标点击(没有我不是一个击键记录器,只是按键速率!为此,我发现我需要使用Windows钩子,我遇到了这个精彩的视频,它给了我一个基本的例。不幸的是,它使用QT框架和许可(和其他时间)的原因,这是目前不可用的我。
所有我需要做的是调整代码,使它不需要return a.exec()行(我相信是从事件循环开始)。

I have been trying to develop a background Windows application in c++ to capture system wide keystrokes and mouse clicks (no I'm not writing a keystroke logger, just keystroke rates!). For this I have figured out that I need to use Windows Hooks and I came across this excellent video which gave me a basic example. Unfortunately, it uses the QT framework and for licencing (and other time based) reasons, this is not available to me currently. All I need to be able to do is adapt the code so that it does not require the "return a.exec()" line (which I believe is what starts off the event loop).

对youtube视频的评论似乎有答案:
对于那些不使用QT的人,只需添加
while(GetMessage (NULL,NULL,0,0));
而不是a.exec(),QT循环它应该工作很好

A comment on the youtube video appeared to have the answer: "For those who don't use QT, just add while(GetMessage(NULL, NULL, 0, 0)); instead of a.exec(), QT loop. It should work fine"

这个解决方案工作。非常感谢帮助下面的代码执行作为事件循环,而不依赖于QT框架。

But could not get this solution to work. Would be very grateful for help in getting the code below to execute as an event loop without depending on the QT framework.

#include <QtCore/QCoreApplication>
#inlcude <QDebug>
#include <iostream>
#include <Windows.h>

HHOOK hHook = NULL;

using namespace std;

LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM     lParam)
{
    qDebug() << "A Key was pressed";
    return CallNextHookEx(hHook, nCode, wParam, lParam);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv); //need to remove this QT dependency

    hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL, 0);
    if (hHook==NULL){
        qDebug() << "Hook Failed";
    }

    return a.exec(); //need to remove this QT dependency
}

())使用消息循环并删除QT引用重写:

Here is an attempt (only showing main()) of re-writing using a message loop and removing the QT references:

int main(int argc, char *argv[])
{
   MSG msg = NULL;
   while (GetMessage(msg, NULL, NULL, NULL)){   
    hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL, 0);
    if (hHook==NULL){
        qDebug() << "Hook Failed";
    }

   }
}


推荐答案

这是一个工作的示例。

#include <Windows.h>
#include <stdlib.h>
#include <iostream>

HHOOK g_hHook = NULL;
DWORD g_HookThread;

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    PKBDLLHOOKSTRUCT hookstruct = reinterpret_cast<PKBDLLHOOKSTRUCT>(lParam);
    std::cout << hookstruct->vkCode << std::endl;
    if( wParam == WM_KEYUP ) {
        if( hookstruct->vkCode == VK_ESCAPE ) {
            PostThreadMessage( g_HookThread, WM_QUIT, NULL, NULL );
        }
    }
    return CallNextHookEx( g_hHook, nCode, wParam, lParam );
}

int main()
{
    g_HookThread = GetCurrentThreadId();
    g_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0);

    MSG msg;
    while( GetMessage(&msg, NULL, NULL, NULL) ) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    UnhookWindowsHookEx(g_hHook);
    return 0;
}

编辑: whatpulse切换到directinput,但现在他们似乎已切换回低层钩子。

edit: I originally commented that whatpulse switched to directinput, but now they seem to have switched back to lowlevel hooks.

这篇关于运行C ++事件循环,不使用QT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆