-
April 9th, 2010, 10:22 AM
#1
can't send WM_KEYDOWN correctly?
Hi,everyone!
I'm tring to write a program to send key press message to other program(such as notepad).I mean if I press "M" in my program,this "M" will append to the notepad text entry.
here is my code:
hwnd = FindWindow(NULL,"Untitled - Notepad");
if(hwnd!=0){
char buffer[100] = "";
GetWindowText(hwnd,buffer,50);
MoveWindow(hwnd,0,0,500,500,TRUE);
SendMessage(hwnd,WM_KEYDOWN,VK_NUMPAD0,0);
SendMessage(hwnd,WM_KEYUP,VK_NUMPAD0,0);
printf("%d\n%s\n",hwnd,buffer);
}else{
printf("%d\n",hwnd);
printf("failure\n");
}
After running this program,the MoveWidnow() function worked,but there isn't any character in notepad text entry...
What should I do to solve this problem?
Any suggestion is appreciated!
Last edited by linkstack; April 9th, 2010 at 10:35 AM.
-
April 9th, 2010, 12:13 PM
#2
Re: can't send WM_KEYDOWN correctly?
Use SendInput API instead.
Victor Nijegorodov
-
April 11th, 2010, 02:46 PM
#3
Re: can't send WM_KEYDOWN correctly?
Consider keybd_event() function, it emulates keyboard events, but it will work only if your window is focused.
Use WM_SETTEXT message to add text into that window, it has not to be focused.
-
April 12th, 2010, 02:44 AM
#4
Re: can't send WM_KEYDOWN correctly?
Originally Posted by davidk
Consider keybd_event() function, it emulates keyboard events, but it will work only if your window is focused.
keybd_event is obsolete and should not be used in Win32 applications. SendInput API should be used instead.
Victor Nijegorodov
-
April 14th, 2010, 10:38 AM
#5
Re: can't send WM_KEYDOWN correctly?
thanks for your reply,but when I'm compiling the program,following error occurs:
KEYBDINPUT undefined
-
April 14th, 2010, 10:43 AM
#6
Re: can't send WM_KEYDOWN correctly?
Have a look at this thread
Victor Nijegorodov
-
April 20th, 2010, 09:59 AM
#7
Re: can't send WM_KEYDOWN correctly?
sorry for my no reply.
now I can use SendInput(),but after I invoked the sendinput(),there isn't anything happend in notepad
this is my code:
KEYBDINPUT kb={0};
kb.wVk = 0x46;
kb.wScan = 0;
kb.dwFlags = KEYEVENTF_UNICODE; // KEYEVENTF_UNICODE=4
INPUT input = {0};
input.type = INPUT_KEYBOARD;
input.ki = kb;
SendInput(1,&input,sizeof(input));
-
April 20th, 2010, 10:07 AM
#8
Re: can't send WM_KEYDOWN correctly?
Originally Posted by linkstack
sorry for my no reply.
now I can use SendInput(),but after I invoked the sendinput(),there isn't anything happend in notepad
this is my code:
Code:
KEYBDINPUT kb={0};
kb.wVk = 0x46;
kb.wScan = 0;
kb.dwFlags = KEYEVENTF_UNICODE; // KEYEVENTF_UNICODE=4
INPUT input = {0};
input.type = INPUT_KEYBOARD;
input.ki = kb;
SendInput(1,&input,sizeof(input));
From MSDN:
KEYEVENTF_UNICODE Windows 2000:
If specified, the system synthesizes a VK_PACKET keystroke. The wVk parameter must be zero. This flag can only be combined with the KEYEVENTF_KEYUP flag. For more information, see the Remarks section.
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|