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!
Re: can't send WM_KEYDOWN correctly?
Use SendInput API instead.
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.
Re: can't send WM_KEYDOWN correctly?
Quote:
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.
Re: can't send WM_KEYDOWN correctly?
thanks for your reply,but when I'm compiling the program,following error occurs:
KEYBDINPUT undefined
Re: can't send WM_KEYDOWN correctly?
Have a look at this thread
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));
Re: can't send WM_KEYDOWN correctly?
Quote:
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:
Quote:
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.