|
I need to send keystrokes to another running process. I already have the hwnd to the process to which I want to send the keystrokes. I'm using this to send the keystrokes. It doesn't work:
::SendMessage(m_fwhs.hWndFound, WM_KEYDOWN, nChar, MAKELPARAM(MAKEWORD(0,(byte)nChar), 0));
::SendMessage(m_fwhs.hWndFound, WM_KEYUP, nChar, MAKELPARAM(MAKEWORD(1,(byte)nChar), MAKEWORD(0,8)));
Can anyone provide any clues?
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
My first guess is that SendMessage() is the problem. Normally a message loop converts WM_KEYDOWN/WM_KEYUP pairs into WM_CHAR or WM_SYSCHAR (that's what TranslateMessage() does). When you SendMessage() a WM_KEYDOWN, your app blocks until the message is processed by the target app. Since the WM_KEYUP isn't in its queue, the result is foobared.
Try changing it to PostMessage().
</wildguess>
--Mike--
http://home.inreach.com/mdunn/
"Not our fault we are intellectually superior to the rest of the office." -- Paul Watson in the Lounge, 12/12/2001
Sonork - 100.10414 AcidHelm
|
|
|
|
|
I had this problem before, and I found some code here http://wwwthep.physik.uni-mainz.de/~frink/nt.html that helped. Scroll to the middle of the page and look for "pushkeys for C".
Hope this helps.
"Am I talking too fast, or are you just playing dumb? If you want I can write it down." -Jarvis Cocker/Pulp
|
|
|
|
|
Recently I ummm ... figured out ... how Microsoft did SendKeys and it is just a bunch of keybd_event calls. So IMHO, just go with the link Daniel listed. It has some examples.
Tim Smith
Descartes Systems Sciences, Inc.
|
|
|
|
|
Well yes but with keybd_event you have to rely on the app having focus - the classic vb example calls AppActivate before SendKeys - might be nicer without that.
|
|
|
|
|
Hi,
This example is really a good example but while using it be warned that it is US Locale specific! It may not work with other keyboard locales!
Kind regards
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
Got caught up in this. Yep, I think PostMessage helps (as you may have already seen), and setting the proper transition flag - on for WM_KEYUP. I think you already have the repeat count right.
The ugly processor hogging code below also borrows from the sample mentioned to get the scan code and stuff, but I don't think the scan is needed.
This code doesn't check the alt, shift, ctrl keys etc, but I assume it could send things like Alt+F4 if it did. Could be one for the FAQ with a bit of work. - 'Write a SendKeys(HWND, LPCTSTR) function...'
Note the elegant hardcoding of the hwnd (er... Calculator )
#include "stdafx.h"
#include "crtdbg.h"
#include "conio.h"
#include "windows.h"
#include "winbase.h"
int main(int argc, char* argv[])
{
HWND hwnd = (HWND) 0x025c;
INT Vk;
INT Scan;
CHAR character[2];
CHAR oemchar[2];
CHAR ch;
do {
while(!_kbhit());
ch = getch();
character[0]=ch;
character[1]='\0';
Vk = VkKeyScan(character[0]) & 0xFF;
CharToOem(character, oemchar);
Scan = OemKeyScan(oemchar[0]) & 0xFF;
printf("Key = %c, VK code = %d, scan = %d\n", ch, Vk, Scan);
UINT lKeydata = 0;
UINT mask = 1;
lKeydata |= mask;
lKeydata |= Scan << 15;
::PostMessage(hwnd, WM_KEYDOWN, Vk, lKeydata);
lKeydata |= mask;
lKeydata |= Scan << 15;
lKeydata |= mask << 31;
::PostMessage(hwnd, WM_KEYUP, Vk, lKeydata);
}
while(1);
return 0;
}
|
|
|
|
|
That helped A LOT! Only problem is it doesn't support shifted keys (capital letters, etc). Is there something special I have to do?
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
I _think_ the first thing I'd try is sandwiching the key to be shifted (or ctrl'd or alt'd) between the up and down messages for the VK_SHIFT (or VK_CONTROL, VK_MENU).
send keydown for shift
send keydown for char
send keyup for char
send keyup for shift
If the char gets there before the shift key goes up it should look like the user pressed it while the key was held down.
Hmmm... should work, but why is my spidey sense tingling...
|
|
|
|
|
My view class is richeditview ,and users enter some string
into it,the length of string is unlimited,it maybe
become one page or two page or...How should I set the attribute
for printing?I mean how does my application should determine
the legnth and size of printing pages and print it correctly
in each page?(something like ms word or notepad do)
|
|
|
|
|
Hi.
I often post questions about C++ status relative to other programming languages as well as how to get specific programming working under C++.
I want to switch direction for once and ask about good programming technique. I am at a point where programming with C++ is no longer intimidating unless it involves something that requires creating an entirely new data structure.
How do you define good programming technique?
Ultimate, the better the programming technique including program design, the more efficient the final program will operate. Furthermore, the program file should be smaller while undoubtedly producing the same result. I often find myself implementing if/else, while/do while and using bool as a "count" to manage the if/else and loops. In terms of effective programming, is that good practice? Do not get me wrong. I use only what is required and nothing more. It is just that sometimes the section of the program I work on need that kind of technique. I would like inputs from experience programmer about implementations that increase performance.
Last, I read Deitel & Deitel C++ How to Program and Accelerat C++ by Andrew Koenig and Barbara E. Moo quick because it is such a short book. I prefer Deitel & Deitel C++ How to Program because it is a complete book. I have never come across a more complete and effective C++ book such as C++ How to Program. Nonetheless, I find Koenig and Moo implementing more involved programming techniques such as using more STL in function parameters, returning STL and lastly calling functions within functions. Maybe these technique seems different because of the way Koenig and Moo wrote their book when. They seems to like everything compact.
Kuphryn
|
|
|
|
|
In the real world there are no hard and fast rules about what is best.
Does your software require speed?
Does your software need a short "to market" time frame?
Does your software require a long support requirement?
Does your software require a small memory footprint?
etc...
The answers to all these question have significant effects on what is considered good programming style.
Making a program small and fast might be counter productive if it requires long term support. Also, making it small and fast might make the product worthless if the time required to do so allows your competition to beat you to market.
In my world, maintainability and flexibility are the two most important things I have to consider. Being fast is important to some time critical sections of the code. Having a small memory footprint is only important when embedding our package.
Case #1: I worked with a programmer who spent a day making the keyboard input software very fast and efficient. On the surface, this sounds great. But when you look at package at a whole, keyboard input was not slow to the user. Our package didn't require much keyboard input. The amount of CPU time being saved was minuscule compared to the CPU time being used by the package as a whole.
Case #2: I worked on a routine that was about 150-200 lines long. The routine was basically the same block of code repeated 2 or 3 times. To make the routine more streamline, I combined the repeated blocks into a generic single block that executed 2 or 3 times. Two weeks later a co-worker brought the code to me and asked me what the hell it was doing. After looking at the code, we both agreed that even though my new routine was much more 'elegant', it was in fact a maintenance nightmare and in the long run was much worse than the original code.
Case #3: A young grad student that I worked with decided that he hated how slash screens would never repaint while the program was still initializing. He spent all day creating a thread in the program just so this one splash screen would paint properly. Of course, this was a total wasted day since the end user would see the splash screen once a day. Also, since this was the only application running on this computer, there weren't going to be any other windows causing the splash screen to repaint.
All three of these cases all boil down to the same thing. Wasted time which means wasted money. Every dollar we waste doing the wrong thing for our software is a dollar that has to come from somewhere else. This money all has to come from somewhere. Either you (the employees/employers) pay for it or the end user does.
What a lot of people don't realize is that the software decisions we make now also affect how much money will be wasted after the product ships. If you have to get it out fast, you will pay for that speed during the support phase.
Oh well, enough rambling.
Tim Smith
Descartes Systems Sciences, Inc.
|
|
|
|
|
kuphryn wrote:
Ultimate, the better the programming technique including program design, the more efficient the final program will operate. Furthermore, the program file should be smaller while undoubtedly producing the same result.
That sounds like you try to hand-optimize your C code yourself. Unless you're writing time-critical or space-critical code (such as a new Quake III-killing 3D engine, an anti-virus background scanner or a new memory allocator), hand-optimizing is a waste of your own time. Let the compiler writers (like MS, with its mounds of resources, experience, and money) handle the optimizing. Your own optimizations are probably harder for others to read and maintain, as well as possibly confusing the compiler's optimization algorithms.
kuphryn wrote:
I often find myself implementing if/else, while/do while and using bool as a "count" to manage the if/else and loops. In terms of effective programming, is that good practice?
If you're using some funky replacement for an if/else, instead of just using if/else, and the code doesn't fall into a critical type I mentioned earlier, run some performance tests on the code. Then rewrite it using normal if/else structures. If there's no meaningful difference, ditch the funky stuff.
--Mike--
http://home.inreach.com/mdunn/
"Not our fault we are intellectually superior to the rest of the office." -- Paul Watson in the Lounge, 12/12/2001
Sonork - 100.10414 AcidHelm
|
|
|
|
|
Thanks guys.
Those are a couple of powerful insights.
Michael Dunn:
Okay. I use bool to as a way to validate if/else and/or while/do whlie loops.
Example:
if (!verify)
...
while(!verify)
...
I agree that it is best to sometime leave it up to C++ itself to optimize the code. However, as programmers, we design the software. It is ultimate up to the programmer to design it in a way that would be best for the job the program performs.
Tim Smith:
Thanks for the clear insight about real world programming. From what you said, I believe the message was it is up to the programmer to manage her/his time wisely in terms of considering which part of an application to put the most time and effort for optimization. The big picture is still about money and getting the product quicker than competitors will still producing a quality software package.
Nicely said!
Kuphryn
|
|
|
|
|
Is there a benchmark for program optimization (execution and processing time)?
Kuphryn
|
|
|
|
|
Well, if you have more cash than you know what to do with, there are packages like Intel's VTune and Rationals Quantify.
You can also use something like PerfTimer to measure performance.
Tim Smith
Descartes Systems Sciences, Inc.
|
|
|
|
|
|
Programs like VTune help you find out where most of the time is being spent.
You can also roll your own timing functions to determine how long specific sections of your code is taking.
Todd Smith
|
|
|
|
|
Thanks.
I believe The Code Project has a class, PerfTime, in its archive that does the job.
Kuphryn
|
|
|
|
|
I'd be very happy if anyone could give me some sort of solution to the following problem:
I am making an .exe file that exports a few function.
The problem I ran into is that if I call malloc inside one of those functions it always returns NULL .
My guess is that no local heap has been initialized, as the C runtime initialization code get's confused by the fact that it's an exe (and therefor no dll entry-point function is called)
If anyone could tell me how to come around this (I need to be able to use the new operator, which calls malloc , which uses _crtheap , but the latter is not exported from the c runtime dll).
Enjoy!
Marc
|
|
|
|
|
|
No, this is not true.
Functions exported by exe files can be used in exactly the same way as those exported by regular dlls
Enjoy!
Marc
|
|
|
|
|
How are you doing to call the exported functions from another executable? Like a normal DLL, loading with LoadLibrary and obtaining the function poitners with GetProcAddress ?
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I want to integrate a CButton Object, a simple Button, into the Mainframe of my Application at Runtime.
Thank you for your Help Alex
|
|
|
|
|
I use Chris Maunders CSystemTray class and don't want the taskbar icon or what to say... Not the icon down there beside the clock!
HOw to hide it??
------------------------------
©0d3 ©®4©k3® - That's me!
------------------------------
|
|
|
|
|