Sending text to chat via Edit
-
I have a 3rd party program which contains a chat area. I am trying to create a function which sends a message to that chat. I have the handle to the parent window and the handle to the Edit window where you type. I am not having any trouble setting the text in the Edit window, my issue is sending the text. All I want to do is be able to send the box a simulated Return key press, but I'm having a lot of issues with it and can't seem to get it to work. I spied the window and when a real Return is pressed, it generates two messages:
P WM_KEYDOWN nVirtKey:VK_RETURN cRepeat:1 ScanCode:1C fExtended: 0 fAltDown: 0 fRepeat: 0 fUp: 0 P WM_KEYUP nVirtKey:VK_RETURN cRepeat:1 ScanCode:1C fExtended: 0 fAltDown: 0 fRepeat:1 fUp:1
For KEYDOWN, the parameters are: wParam: 0000000D lParam: 001C0001 For KEYUP, the parameters are: wParam: 0000000D lParam: C01C0001 Given all this information, my current function looks like:void say( const char* toSay ) { HWND tempa = SetActiveWindow( hwnd ); HWND tempw = SetFocus( chatBox ); Sleep(500); SendMessage( chatBox, WM_SETTEXT, (WPARAM)0, (LPARAM)toSay ); Sleep(1000); PostMessage( chatBox, WM_KEYDOWN, VK_RETURN, 0x001C0001 ); Sleep(2000); PostMessage( chatBox, WM_KEYUP, VK_RETURN, 0xC01C0001 ); Sleep(2000); SetFocus( tempw ); SetActiveWindow( tempa ); }
I have had very little success with this method. It will sometimes send the first message it gets but then will no longer send anything. It seems like this should be much simpler, as I'm sure it's a common need. Any help is greatly appreciated. -
I have a 3rd party program which contains a chat area. I am trying to create a function which sends a message to that chat. I have the handle to the parent window and the handle to the Edit window where you type. I am not having any trouble setting the text in the Edit window, my issue is sending the text. All I want to do is be able to send the box a simulated Return key press, but I'm having a lot of issues with it and can't seem to get it to work. I spied the window and when a real Return is pressed, it generates two messages:
P WM_KEYDOWN nVirtKey:VK_RETURN cRepeat:1 ScanCode:1C fExtended: 0 fAltDown: 0 fRepeat: 0 fUp: 0 P WM_KEYUP nVirtKey:VK_RETURN cRepeat:1 ScanCode:1C fExtended: 0 fAltDown: 0 fRepeat:1 fUp:1
For KEYDOWN, the parameters are: wParam: 0000000D lParam: 001C0001 For KEYUP, the parameters are: wParam: 0000000D lParam: C01C0001 Given all this information, my current function looks like:void say( const char* toSay ) { HWND tempa = SetActiveWindow( hwnd ); HWND tempw = SetFocus( chatBox ); Sleep(500); SendMessage( chatBox, WM_SETTEXT, (WPARAM)0, (LPARAM)toSay ); Sleep(1000); PostMessage( chatBox, WM_KEYDOWN, VK_RETURN, 0x001C0001 ); Sleep(2000); PostMessage( chatBox, WM_KEYUP, VK_RETURN, 0xC01C0001 ); Sleep(2000); SetFocus( tempw ); SetActiveWindow( tempa ); }
I have had very little success with this method. It will sometimes send the first message it gets but then will no longer send anything. It seems like this should be much simpler, as I'm sure it's a common need. Any help is greatly appreciated.watch out this http://www.codeproject.com/dialog/keystroke.asp[^]
-
watch out this http://www.codeproject.com/dialog/keystroke.asp[^]
Thanks for the link Vikram, however it seems that there is a bug with that engine (and the user comments seem to confirm) that creates problems with XP. From what I saw in the source of that project, I thought that this would work:
void say( const char* toSay ) { if( !SetForegroundWindow( chatBox ) ){ cout << "COULD NOT SET CHATBOX TO ACTIVE" << endl; } Sleep(500); UINT scan = MapVirtualKey( VK_RETURN, 0 ); Sleep(500); SendMessage( chatBox, WM_SETTEXT, (WPARAM)0, (LPARAM)toSay ); Sleep(2000); keybd_event(VK_RETURN, scan, KEYEVENTF_EXTENDEDKEY | 0, 0 ); Sleep(2000); keybd_event(VK_RETURN, scan, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); }
However, that doesn't seem to work. As in my previously posted function, the chatBox is set with the text and when VK_RETURN is sent it does clear, but it does not show up in the chat, it just gets cleared. I'm not sure why this is. -- modified at 3:43 Saturday 17th December, 2005