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
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    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.

  2. #2
    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.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    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

  4. #4
    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

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Windows hook using windows api in QT

    yes i am not very well in c++
    I do not know if you have experience with Python, but many of your mistakes make me think so. In Python, you can "print" anything, and the Python interpreter will find a way to convert the object into a printable string. So in Python, you can write "print( cod )" and the interpreter will convert "cod" into a string and print it.

    You can't do the same thing in C++. In C++, -you- must convert the object into a string before it can be printed. "cod" is an integer, so you have to convert it using QString::num() before trying to print it. A cast -is not- conversion. Using (const char *)cod does not convert cod to a string. It tells C++ "cod is actually not an int, it is a pointer to a const char, so please use it that way". Of course, that is not true. "cod" is still an int, and trying to use it like it was a pointer to a string will cause a crash.
    <=== 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.

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

    Thumbs up Re: Windows hook using windows api in QT

    this was the problem that i have with other things. this is the biggest problem that i have and you explain me very well stranz
    Last edited by davinciomare; 5th October 2016 at 22:56.

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

    Default Re: Windows hook using windows api in QT

    so everytime if i want to print something i must to convert to string ok and write in file. And if for example i have one char the conversion? ok so i must do always the same conversion char take me problems but it's ok this was my problem. I didnt understand this.

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

    Default Re: Windows hook using windows api in QT

    Read about QTextStream.

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.