Results 1 to 12 of 12

Thread: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

  1. #1
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Question How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    I've written a code which receives images from a webcam by using OpenCV library. In this program, I try to recognize an object (for example an English letter). My application recognizes the letter. Suppose I've opened a word processor such as Notepad or MS Word, and it is running simultaneously with my mentioned Qt application. I'd like my Qt application to emulate keyboard. I mean, for example, when my application detects there is a "B" is in front of the webcam,it will emulate pressing "B" by keyboard, and for example if Notepad window is in focus, a "B" letter will be written in it. I also would like to be able to emulate pair keys, such as "Ctrl+A".

    Is there any way to do this?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    This is very platform specific, and even specific to the applications you want to run (such as which word editor).
    If you are under windows, google for DDE.

    You can solve the issue rather easy by providing your own editor, from within your own application.
    Then this is a trivial matter, and will be consistent over all Qt supported platforms.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    tofighi (25th May 2011)

  4. #3
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    I think I found a solution, but there is another problem. I found this:
    http://www.codeproject.com/KB/cpp/se...p_Article.aspx
    Now there is another problem in compiling the code:
    error: cannot convert 'const char*' to 'const TCHAR*'

    Is it possible to correct this useful class for Qt ?

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    Now there is another problem in compiling the code:
    error: cannot convert 'const char*' to 'const TCHAR*'
    You have supplied no information as to what you where to doing, so there is no way to say what is wrong, apart from the obvious error that the complier is reporting - namely, a type conversion problem, probably due to TCHAR not defined in your code, probably due to missing includes.
    Try adding #include <windows.h>
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #5
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    Quote Originally Posted by high_flyer View Post
    You have supplied no information as to what you where to doing, so there is no way to say what is wrong, apart from the obvious error that the complier is reporting - namely, a type conversion problem, probably due to TCHAR not defined in your code, probably due to missing includes.
    Try adding #include <windows.h>
    As I mentioned, this error is the main problem of this program (Please see the source code to understand where it will trigger an error and help if there is anyway for modifying the program for Qt). This program has above 100 errors, mainly because there is a conversion which is not accepted in Qt.

    I found a way which is not a perfect one but it works in Windows. The trick is executing a python script using QProcess. In this way, user should install python on his/her machine. The python module which I've used is SendKeys.
    In this way, we can use python scripting for emulating keyboard in Windows.

    I hope this helps someone.
    Last edited by tofighi; 26th May 2011 at 01:45.

  7. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    , mainly because there is a conversion which is not accepted in Qt.
    Again, TCHAR is a windows type.
    You need to include the correct windows header for that type.
    Did you try including windows.h?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #7
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    Quote Originally Posted by high_flyer View Post
    Again, TCHAR is a windows type.
    You need to include the correct windows header for that type.
    Did you try including windows.h?
    Yes, I've included it. There is an include directive in sendkeys.h :
    Qt Code:
    1. #include <windows.h>
    2. #include <tchar.h>
    To copy to clipboard, switch view to plain text mode 
    I also want to share my Qt program which doesn't work because of that error I've mentioned. please take a look at this project and let me know if I can change something which helps me run the program.
    The purpose of this program is running notepad using Sendkeys class:
    QtSendKey.zip
    You can read more information about the Sendkeys class here.

  9. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    Show the line of code that generates the error. If all you need is to call WinApi function that expects TCHAR *, you can use the L"" macro:
    Qt Code:
    1. HRESULT someWinApiMethod( TCHAR * string );
    2. //...
    3. someWinApiMethod(L"a wide unicode string constant");
    To copy to clipboard, switch view to plain text mode 
    As for QStrings, you can use toWCharArray and fromWCharArray conversion methods.

  10. The following user says thank you to stampede for this useful post:

    tofighi (28th May 2011)

  11. #9
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    Quote Originally Posted by stampede View Post
    Show the line of code that generates the error. If all you need is to call WinApi function that expects TCHAR *, you can use the L"" macro:
    Qt Code:
    1. HRESULT someWinApiMethod( TCHAR * string );
    2. //...
    3. someWinApiMethod(L"a wide unicode string constant");
    To copy to clipboard, switch view to plain text mode 
    As for QStrings, you can use toWCharArray and fromWCharArray conversion methods.
    That's a good suggestion. I convert some strings with this method and errors reduce. There are some other errors that I don't know how to solve:
    for example

    Qt Code:
    1. int cmp = _tcsnicmp(KeyNames[Middle].keyName, KeyString, _tcslen(KeyString));
    2. error: cannot convert 'TCHAR*' to 'char*' for argument '1' to 'char* strncpy(char*, const char*, size_t)'
    3.  
    4. while (*p && *p != _TXCHAR('}'))
    5. p++;
    6. error: '_TXCHAR' was not declared in this scope
    7.  
    8. _tcscpy(titleclass, WindowTitle);
    9. error: cannot convert 'TCHAR*' to 'const char*' for argument '1' to 'char* strstr(const char*, const char*)'
    To copy to clipboard, switch view to plain text mode 

    I suggest an expert, if has time, see the attached code in previous post and debug it. I'm sure it is a very useful program which can help many.

  12. #10
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    Is it possible someone look at this again?

  13. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    error: '_TXCHAR' was not declared in this scope
    You need to include tchar.h
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #12
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to trigger a hotkey or shortcut or emulate keyboard with Qt?

    Quote Originally Posted by high_flyer View Post
    You need to include tchar.h
    I did it before. Is it possible to ask you kindly download the program, and compile it to see errors? This program is very useful and I know it will help many to create HCI programs using Qt.

Similar Threads

  1. Keyboard shortcut event not received
    By D Cell in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2010, 09:52
  2. Replies: 3
    Last Post: 23rd August 2010, 10:37
  3. Additional keyboard shortcut for a menu
    By negritot in forum Qt Programming
    Replies: 5
    Last Post: 17th September 2009, 05:58
  4. Invoke QCompletion on keyboard shortcut
    By aspidites in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2009, 10:32
  5. Rerouting messages for keyboard shortcut selection dialog.
    By andy.fillebrown in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:55

Tags for this Thread

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.