Implementation of simple remote control (passing mouse and keyboard messages only)

Source: Internet
Author: User

Assume that two identical applications run on the same operating system. To achieve remote control, you can transmit the mouse and keyboard messages to the other party, after receiving the message, the recipient can parse the mouse and keyboard message and execute it.

Below are several key procedures:

The first is to process received messages, which should be placed in socket reception or serial port reception: (Be careful when you right-click the following and double-click it !)

//// Parse the message sent from the client and send it to the local message queue // void dispatchwmmessage (char * szstring, void * P) {ctestdlg * pdlg = (ctestdlg *) p; // mouse message struct {char * szwmmousemsg;} wmmousemsg [] = {"wm_mm", "wm_lbd", "wm_lbu", "wm_lbk", "wm_mbd ", "wm_mbu", "wm_mbk", "wm_rbd", "wm_rbu", "wm_rbk"}; // Keyboard Message struct {char * szwmkeybdmsg ;} wmkeybdmsg [] = {"wm_kd", "wm_ku"}; // common message: color mode, number of grids, and compressed message struct {char * szmsg ;} MSG [] = {"wm_comp", "wm_grid", "wm_cmod "}; Intnwmmousemsg; struct; intnmsg; structcommandlistcommandstart; structcommandlist * pcommandnode; structcommanddscommand; char * pdest; intiloc, nchar; intiloop, iparms; charszstring2 [2049]; // mouse, keyboard, number of common messages nwmmousemsg = (INT) (sizeof (wmmousemsg)/sizeof (wmmousemsg [0]); nwmkeybdmsg = (INT) (sizeof (wmkeybdmsg) /sizeof (wmkeybdmsg [0]); nmsg = (INT) (sizeof (MSG)/sizeof (MSG [0]); // initialize Command linked list commandstart. pnex T = NULL; pcommandnode = & commandstart; // analyze the command. The command parameters iparms = 0 are intercepted. While (pdest = strchr (szstring ,';')) {iloc = pdest-szstring; nchar = iloc; memset (command. szelement, '\ 0', sizeof (command. szelement); strncpy (command. szelement, szstring, nchar); // send to the Command Stack pcommandnode = add_command (pcommandnode, command); memset (szstring2, '\ 0', sizeof (szstring2 )); strcpy (szstring2, & szstring [iloc + 1]); strcpy (szstring, SZ String2); iparms ++; If (iparms = 5) // each command has five parameters break;} // The Processing Command pcommandnode = commandstart. pnext; If (pcommandnode) {// uintkeyflags; intimessage; intfwmmousemsg; dworddwx, dwy; // The Keyboard Message intfwmkeybdmsg; uintvk; intfdown; intcrepeat; uintflags; // determine whether there is a mouse message fwmmousemsg = false; For (iloop = 0; iloop <nwmmousemsg; iloop ++) {If (strcmp (pcommandnode-> command. szelement, wmmousemsg [iloop]. szwmmousemsg) = 0) {// set the mouse message flag Fwmmousemsg = true; // The specific mouse message if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_mm \ 0") = 0) iMessage = 1; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_lbd \ 0") = 0) iMessage = 2; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_lbu \ 0") = 0) iMessage = 3; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_lbk \ 0") = 0) iMessage = 4; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_mbd \ 0") = 0) imess Age = 5; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_mbu \ 0") = 0) iMessage = 6; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_mbk \ 0") = 0) iMessage = 7; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_rbd \ 0") = 0) iMessage = 8; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_rbu \ 0") = 0) iMessage = 9; else if (strcmp (wmmousemsg [iloop]. szwmmousemsg, "wm_rbk \ 0") = 0) iMessage = 10; // move Next node of the parameter stack, X coordinate pcommandnode = pcommandnode-> pnext; dwx = (DWORD) atof (pcommandnode-> command. szelement); // move to the next node of the parameter stack, Y coordinate pcommandnode = pcommandnode-> pnext; dwy = (DWORD) atof (pcommandnode-> command. szelement); // move to the next node of the parameter stack. The secondary key is pcommandnode = pcommandnode-> pnext; keyflags = atoi (pcommandnode-> command. szelement); // exit loop break;} // If a mouse message exists, process the mouse message if (fwmmousemsg) {// process the mouse message if (iMessage = 1) // move the cursor {// MOUs E_event (events | mouseeventf_move,); setcursorpos (dwx, dwy);} else if (iMessage = 2) // left click to press {mouse_event (mouseeventf_absolute | mouseeventf_leftdown, 0, 0);} else if (iMessage = 3) // left-click to raise {mouse_event (mouseeventf_absolute | mouseeventf_leftup,);} // The message with the cursor below is not processed !!! Else if (iMessage = 4) {mouse_event (mouseeventf_absolute | average,); mouse_event (mouseeventf_absolute | average,); mouse_event (mouseeventf_absolute | average, ); mouse_event (mouseeventf_absolute | mouseeventf_leftup,);} else if (iMessage = 5) {mouse_event (mouseeventf_absolute | mouseeventf_middledown );} else if (iMessage = 6) {mouse_event (mouseeventf_absolute | average,);} else if (iMessage = 7) {mouse_event (mouseeventf_absolute | mouseeventf_middledown, ); mouse_event (mouseeventf_absolute | average );} else if (iMessage = 8) {mouse_event (mouseeventf_absolute | mouseeventf_rightdown,);} else if (iMessage = 9) {mouse_event (mouseeventf_absolute | average, 0, 0);} else if (iMessage = 10) {mouse_event (mouseeventf_absolute | mouseeventf_rightdown,); mouse_event (mouseeventf_absolute | mouseeventf_rightup ); mouse_event (mouseeventf_absolute | mouseeventf_rightdown,); mouse_event (mouseeventf_absolute | mouseeventf_rightup );}} else {// if there is no mouse message, determine whether there is a Keyboard Message fwmkeybdmsg = false; For (iloop = 0; iloop <nwmkeybdmsg; iloop ++) {If (strcmp (pcommandnode-> command. szelement, wmkeybdmsg [iloop]. szwmkeybdmsg) = 0) {// set the Keyboard Message flag fwmkeybdmsg = true; If (strcmp (wmkeybdmsg [iloop]. szwmkeybdmsg, "wm_kd \ 0") = 0) iMessage = 1; // press the key else if (strcmp (wmkeybdmsg [iloop]. szwmkeybdmsg, "wm_ku \ 0") = 0) iMessage = 2; // release the key. // move it to the next node of the parameter linked list, with the Ural key code pcommandnode = pcommandnode-> pnext; VK = atoi (pcommandnode-> command. szelement); // move to the next node of the parameter linked list, press the key sign pcommandnode = pcommandnode-> pnext; fdown = atoi (pcommandnode-> command. szelement); // move to the next node of the parameter linked list. The number of key duplicates is pcommandnode = pcommandnode-> pnext; crepeat = atoi (pcommandnode-> command. szelement); // move to the next node of the parameter linked list, with the flag pcommandnode = pcommandnode-> pnext; flags = atoi (pcommandnode-> command. szelement); break ;}}// if a Keyboard Message exists, the system processes the keyboard message if (fwmkeybdmsg) {If (iMessage = 1) // simulate the key message {keybd_event (byte) VK, (byte) VK, 0, 0);} else if (iMessage = 2) // simulate the message {keybd_event (byte) VK, (byte) VK, keyeventf_keyup, 0) ;}} else // general message {for (iloop = 0; iloop <nmsg; iloop ++) {If (strcmp (pcommandnode-> command. szelement, MSG [iloop]. szmsg) = 0) {If (strcmp (MSG [iloop]. szmsg, "wm_comp \ 0") = 0) {// move to the next node of the parameter linked list, compress the level pcommandnode = pcommandnode-> pnext; // icompressionlevel = atoi (pcommandnode-> command. szelement);} else if (strcmp (MSG [iloop]. szmsg, "wm_grid \ 0") = 0) {// move to the next node of the parameter linked list, number of X grids pcommandnode = pcommandnode-> pnext; // ngridx = atoi (pcommandnode-> command. szelement); // move to the next node of the parameter linked list. The number of grid Y is pcommandnode = pcommandnode-> pnext; // ngridy = atoi (pcommandnode-> command. szelement); // clear the current display settings // cleardisplay (hserverwnd); // initialize the new display settings // initdisplay (hserverwnd );} else if (strcmp (MSG [iloop]. szmsg, "wm_cmod \ 0") = 0) {// move to the next node of the parameter linked list, the number of bytes for each screen point pcommandnode = pcommandnode-> pnext; // bmbitspixel = atoi (pcommandnode-> command. szelement); // cleardisplay (hserverwnd); // initdisplay (hserverwnd) ;}}}// clear command queue clear_command (& commandstart );} // Add an element to the Command Stack: struct commandlist * add_command (struct commandlist * pnode, struct commandds command) {If (pnode-> pnext = (struct commandlist *) malloc (sizeof (struct commandlist) {pnode = pnode-> pnext; strcpy (pnode-> command. szelement, command. szelement); pnode-> pnext = NULL; return pnode;} return NULL;} // clear the Command Stack element void clear_command (struct commandlist * pstart) {structcommandlist * pprev; structcommandlist * pnode; while (pnode = pstart-> pnext) {pprev = pstart; pprev-> pnext = pnode-> pnext; free (pnode );}}

The second is the sending code, which can be placed in the pretranslatemessage of the Main Dialog Box (window), or in the pretranslatemessage of the APP:

Bool ctestdlg: pretranslatemessage (MSG * PMSG) {If (PMSG-> message = wm_lbuttondown) // left click to press {If (m_bsend) {cpoint pt; getcursorpos (& pt ); memset (szmsg, '\ 0', sizeof (szmsg); sprintf (szmsg, "wm_lbd; % d; 0; \ 0", PT. x, PT. y, keyflags); lansend (szmsg, strlen (szmsg) ;}} else if (PMSG-> message = wm_lbuttonup) // left-click lift {If (m_bsend) {cpoint pt; getcursorpos (& pt); memset (szmsg, '\ 0', sizeof (szmsg); sprintf (szmsg, "wm_lbu; % d; % d; 0; \ 0 ", PT. x, PT. y, keyflags); lansend (szmsg, strlen (szmsg) ;}} else if (PMSG-> message = wm_mousemove) // move the cursor {If (m_bsend) {point pt;: getcursorpos (& pt); memset (szmsg, '\ 0', sizeof (szmsg); // sprintf (szmsg, "wm_mm; % d; % d; 0; \ 0 ", get_x_lparam (PMSG-> lparam), get_y_lparam (PMSG-> lparam), 0); sprintf (szmsg," wm_mm; % d; 0; \ 0 ", PT. x, PT. y, 0); lansend (szmsg, strlen (szmsg); Return false ;}} else if (PMSG-> message = wm_keydown) // press the keyboard {If (m_bsend) {memset (szmsg, '\ 0', sizeof (szmsg); sprintf (szmsg, "wm_kd; % d; \ 0 ", PMSG-> wparam, 0, 0, 0); lansend (szmsg, strlen (szmsg) ;}} else if (PMSG-> message = wm_keyup) // keyboard lift {If (m_bsend) {memset (szmsg, '\ 0', sizeof (szmsg); sprintf (szmsg, "wm_ku; % d; % d; \ 0 ", PMSG-> wparam, 0, 0, 0); lansend (szmsg, strlen (szmsg);} return cdialog :: pretranslatemessage (PMSG );}

The above lansend function is the socket sending (or serial sending) function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.