Results 1 to 15 of 15

Thread: Windows hook using windows api in QT

Hybrid View

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

    Default Windows hook using windows api in QT

    Hi i am searching for windows hook and i checked this in the webpage of microsoft. But i dont know how i can't return the value since function using LRESULT to show for example since qDebug the key pressed for me in terminal.
    Error:
    C:\Users\moh\Documents\Hook\main.cpp:63: error: C2440: 'return' : no se puede realizar la conversi¢n de 'QString' a 'LRESULT' No hay disponible ning£n operador de conversi¢n definido por el usuario que pueda realizar esta conversi¢n, o bien no se puede llamar al operador

    The question is how i can change the returned value to Qstring. It's only for doing one example with qDebug thx
    Last edited by davinciomare; 4th October 2016 at 23:11. Reason: Conversion Qt

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Windows hook using windows api in QT

    LRESULT is just a long. Read the QString documentation for how to convert integers into printable strings.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #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]

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Windows hook using windows api in QT

    I have no idea what you are trying to do. YOUR CODE IS UNREADABLE. FOR THE LAST TIME, USE [CODE] TAGS.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Default Re: Windows hook using windows api in QT

    what is code tags? where are?

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Windows hook using windows api in QT

    Quote Originally Posted by davinciomare View Post
    what is code tags? where are?
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Question Re: Windows hook using windows api in QT

    Sorry i will not repeat this mistake again.
    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 

    I want to return for example num of LRESULT function and print with qdebug since main or something like that.
    Last edited by davinciomare; 5th October 2016 at 09:51. Reason: question

  8. The following user says thank you to davinciomare for this useful post:

    d_stranz (5th October 2016)

  9. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Windows hook using windows api in QT

    I think that this is not a version of the code with which you write in the first post. Line 63 is empty.

  10. #9
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Windows hook using windows api in QT

    When i use const char* to print with qdebug the program crash.
    Code:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QTime>
    4. #include <QChar>
    5. #include <iostream>
    6. #include <QFile>
    7. #include <QString>
    8. #include <Windows.h>
    9. #pragma comment(lib,"user32.lib")
    10.  
    11. HHOOK hHook = NULL;
    12.  
    13. using namespace std;
    14.  
    15. QByteArray contenido="Estoy escribiendo a un archivo";
    16. QFile ap("C:/Users/moh/Desktop/documento.txt");
    17.  
    18. void UpdateKeyState(BYTE *keystate, int keycode)
    19. {
    20. keystate[keycode] = GetKeyState(keycode);
    21. }
    22.  
    23. LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
    24. {
    25. QFile ap("C:/Users/moh/Desktop/documento.txt");
    26.  
    27.  
    28. //WPARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN or WM_SYSKEYUP
    29. //LPARAM is the key information
    30. // if( wParam == WM_KEYDOWN )
    31. //qDebug() << "Key Pressed!";
    32.  
    33. //Get the key information
    34.  
    35. KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);
    36.  
    37. wchar_t buffer[5];
    38.  
    39. //get the keyboard state
    40. BYTE keyboard_state[256];
    41. GetKeyboardState(keyboard_state);
    42. UpdateKeyState(keyboard_state, VK_SHIFT);
    43. UpdateKeyState(keyboard_state, VK_CAPITAL);
    44. UpdateKeyState(keyboard_state, VK_CONTROL);
    45. UpdateKeyState(keyboard_state, VK_MENU);
    46.  
    47.  
    48.  
    49. //Get Keyboard Layout
    50. HKL keyboard_layout = GetKeyboardLayout(0);
    51.  
    52. //Get the name
    53. char lpszName[0x100] = {0};
    54.  
    55. DWORD dwMsg = 1;
    56. dwMsg += cKey.scanCode << 16;
    57. dwMsg += cKey.flags << 24;
    58.  
    59. int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName,255);
    60. //try to convert the key info
    61. int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer,4,0, keyboard_layout);
    62. buffer[4] = L'\0';
    63. //qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
    64. QString num = QString::fromUtf16((ushort*)buffer);
    65. // Print the output
    66.  
    67. int cod = cKey.vkCode;
    68.  
    69. if( wParam == WM_KEYDOWN ){
    70. //qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
    71. ap.open(QIODevice::WriteOnly | QIODevice::Text);
    72. ap.write((const char*)result);
    73. ap.close();
    74.  
    75.  
    76.  
    77. }
    78.  
    79. return CallNextHookEx(hHook, nCode, wParam, lParam);
    80.  
    81.  
    82. }
    83.  
    84. int main(int argc, char *argv[])
    85. {
    86.  
    87. QCoreApplication a(argc, argv);
    88.  
    89.  
    90. hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL,0);
    91. if(hHook == NULL){
    92. qDebug() << "error";
    93. }
    94.  
    95. /*
    96.   if(!ap.exists()){
    97.   qDebug() << "El archivo seleccionado no existe";
    98.   return 1;
    99.   }
    100.   ap.open(QIODevice::WriteOnly | QIODevice::Text);
    101.   if (!ap.isOpen()){
    102.   qDebug() << "El archivo no se ha podido abrir";
    103.   return 2;
    104.   }
    105.   //contenido = ap.readAll();
    106.   ap.write(contenido);
    107.   ap.flush();
    108.   ap.close();
    109. */
    110.  
    111. return a.exec();
    112. }
    To copy to clipboard, switch view to plain text mode 

    Basically the problem is here:
    ap.write((const char*)cod);

    When i write this the program crash. And i can't return other values only 4 arguments since MyLowLevelKeyBoardProc. Some way to return my key pressed is for learning?
    Last edited by davinciomare; 5th October 2016 at 12:43.

  11. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Windows hook using windows api in QT

    You do not necessarily know what you're doing. "(const char*)cod" is the "cod variable contains the address." I do not think the variable cod was a pointer to memory.
    I guess you must first learn the basics of C ++ and later use libraries as powerful as Qt

  12. #11
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Windows hook using windows api in QT

    yes i am not very well in c++ but i was searching and you right a lot of documentation about this so it's right. thx solved

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.