Results 1 to 15 of 15

Thread: Windows hook using windows api in QT

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Windows hook using windows api in QT

    I know how to convert i need to use this:
    QString tmp = QString::number();

    But i have one more question related with this, i want to return the value for learning and then show with qdebug but i couldnt. Code:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QTime>
    4. #include <QChar>
    5. #include <iostream>
    6. #include <Windows.h>
    7. #pragma comment(lib,"user32.lib")
    8.  
    9. HHOOK hHook = NULL;
    10.  
    11. using namespace std;
    12.  
    13. void UpdateKeyState(BYTE *keystate, int keycode)
    14. {
    15. keystate[keycode] = GetKeyState(keycode);
    16. }
    17.  
    18. LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
    19. {
    20.  
    21. //WPARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN or WM_SYSKEYUP
    22. //LPARAM is the key information
    23. // if( wParam == WM_KEYDOWN )
    24. //qDebug() << "Key Pressed!";
    25.  
    26. //Get the key information
    27.  
    28. KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);
    29.  
    30. wchar_t buffer[5];
    31.  
    32. //get the keyboard state
    33. BYTE keyboard_state[256];
    34. GetKeyboardState(keyboard_state);
    35. UpdateKeyState(keyboard_state, VK_SHIFT);
    36. UpdateKeyState(keyboard_state, VK_CAPITAL);
    37. UpdateKeyState(keyboard_state, VK_CONTROL);
    38. UpdateKeyState(keyboard_state, VK_MENU);
    39.  
    40.  
    41.  
    42. //Get Keyboard Layout
    43. HKL keyboard_layout = GetKeyboardLayout(0);
    44.  
    45. //Get the name
    46. char lpszName[0x100] = {0};
    47.  
    48. DWORD dwMsg = 1;
    49. dwMsg += cKey.scanCode << 16;
    50. dwMsg += cKey.flags << 24;
    51.  
    52. int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName,255);
    53. //try to convert the key info
    54. int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer,4,0, keyboard_layout);
    55. buffer[4] = L'\0';
    56. //qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
    57. int num = cKey.vkCode;
    58. // Print the output
    59.  
    60. if( wParam == WM_KEYDOWN )
    61. //qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
    62. return num;
    63.  
    64.  
    65. return CallNextHookEx(hHook, nCode, wParam, lParam);
    66. }
    67.  
    68. int main(int argc, char *argv[])
    69. {
    70.  
    71. QCoreApplication a(argc, argv);
    72.  
    73. hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL,0);
    74. if(hHook == NULL){
    75. qDebug() << "error";
    76. }
    77. QString tmp = QString::number(num);
    78.  
    79. return a.exec();
    80. }
    To copy to clipboard, switch view to plain text mode 

    It's only for doing one example with qdebug of the webpage of microsoft. sorry again..
    Last edited by anda_skoa; 6th October 2016 at 16:08. Reason: changed [code] to [code]

Similar Threads

  1. Replies: 0
    Last Post: 10th June 2014, 13:38
  2. Replies: 0
    Last Post: 10th October 2013, 07:04
  3. New dialog/windows creation hook
    By gilamran in forum Qt Programming
    Replies: 9
    Last Post: 29th March 2011, 00:01
  4. Replies: 2
    Last Post: 6th September 2010, 14:22
  5. Replies: 3
    Last Post: 12th July 2010, 06:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.