PDA

View Full Version : How to trigger a hotkey or shortcut or emulate keyboard with Qt?



tofighi
23rd May 2011, 18:01
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?

high_flyer
24th May 2011, 09:02
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.

tofighi
25th May 2011, 07:05
I think I found a solution, but there is another problem. I found this:
http://www.codeproject.com/KB/cpp/sendkeys_cpp_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 ?

high_flyer
25th May 2011, 08:41
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>

tofighi
25th May 2011, 23:14
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 (http://www.rutherfurd.net/python/sendkeys/).
In this way, we can use python scripting for emulating keyboard in Windows.
I hope this helps someone.

high_flyer
26th May 2011, 08:55
, 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?

tofighi
28th May 2011, 10:38
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 :

#include <windows.h>
#include <tchar.h>
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:
6488
You can read more information about the Sendkeys class here (http://www.codeproject.com/KB/cpp/sendkeys_cpp_Article.aspx).

stampede
28th May 2011, 11:28
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:


HRESULT someWinApiMethod( TCHAR * string );
//...
someWinApiMethod(L"a wide unicode string constant");

As for QStrings, you can use toWCharArray (http://doc.qt.nokia.com/latest/qstring.html#toWCharArray) and fromWCharArray (http://doc.qt.nokia.com/latest/qstring.html#fromWCharArray) conversion methods.

tofighi
28th May 2011, 14:06
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:


HRESULT someWinApiMethod( TCHAR * string );
//...
someWinApiMethod(L"a wide unicode string constant");

As for QStrings, you can use toWCharArray (http://doc.qt.nokia.com/latest/qstring.html#toWCharArray) and fromWCharArray (http://doc.qt.nokia.com/latest/qstring.html#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


int cmp = _tcsnicmp(KeyNames[Middle].keyName, KeyString, _tcslen(KeyString));
error: cannot convert 'TCHAR*' to 'char*' for argument '1' to 'char* strncpy(char*, const char*, size_t)'

while (*p && *p != _TXCHAR('}'))
p++;
error: '_TXCHAR' was not declared in this scope

_tcscpy(titleclass, WindowTitle);
error: cannot convert 'TCHAR*' to 'const char*' for argument '1' to 'char* strstr(const char*, const char*)'


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.

tofighi
15th July 2011, 20:43
Is it possible someone look at this again?

high_flyer
18th July 2011, 13:51
error: '_TXCHAR' was not declared in this scope
You need to include tchar.h

tofighi
23rd July 2011, 20:58
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.