[FZH] C/C++ 请教控制鼠标的库/头文件等

Guannan Ma mythmgn在gmail.com
星期日 五月 8 17:24:05 UTC 2011


:-).

我是通过手机putty wifi连接到自己笔记本实现的.
代码如下 今天晚上写出来个大概  测试了一下  可以使用  嘿嘿


#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>

#include <xdo.h>


#define POS_PLUS 5
#define D_POS_PLUS 50

/** !@description
   Nokia Symbian S60 V3

   Arrow key UP  ---->  27 91 65
   Arrow key LEFT ----> 27 91 68
             Down ---->  27 91 66
                         Right --->  27 91 67

   Confirm key----> 10 (simulate pressing enter)

 *
 *
 *
 *
 *
 */

int setTTY() {

   int ttyDevice = STDOUT_FILENO;

   printf("%s\n","Noncanonical terminal mode is enabled");
   /* [-]icanon
             enable erase, kill, werase, and rprnt special characters*/
   /*min N  with -icanon, set N characters minimum for a completed read */
   system("stty   -icanon");

   printf(">");
   /* Make sure file descriptor is for a TTY device.   */
   if ( ! isatty(ttyDevice) ) {
       printf("Not a TTY device.n");
       return(EXIT_FAILURE);
   }
   /* Flush both the input and output queues.          */
   else {
       if (tcflush(ttyDevice, TCIOFLUSH) == 0)
           ;
       //printf("The input and output queues have been flushed\n");
       else
           perror("tcflush error");
   }
}
int nonCanonical(int sign) {
   struct termios ttystate;
   //get the terminal state
   tcgetattr(STDIN_FILENO, &ttystate);
   if (sign==1) {
       /*turn off canonical mode*/
       ttystate.c_lflag &= ~ICANON; /* To diable canonical input*/
       ttystate.c_lflag &= ~ECHO;  // To disable user see what he types
       /*ttystate.c_lflag &= ~ISIG;  To disable Control signals using
control Keys
       minimum of number input read.*/
       ttystate.c_cc[VMIN] = 1;
   } else {
       /*turn on canonical mode*/
       ttystate.c_lflag |= ICANON; /*To enable Canonical mode*/
       ttystate.c_lflag |= ECHO;  /*To enable user see what he pressed*/

   }
   /*set the terminal attributes.*/
   tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
   return 1;
}

int handlerKeyPress(char *display) {
   static fd_set rfds;
   struct timeval tv;
   char a,b,c;
   char str[20];

   xdo_t *p_xt=xdo_new(display);
   while (1==1) {
       // Set up stdin buffer probe
       FD_ZERO(&rfds);
       FD_SET(0, &rfds);

       tv.tv_sec = 0;
       tv.tv_usec = 0;
       if (!select(1, &rfds, NULL, NULL, &tv)) {
           // No input available
           //skips++;
       } else {
           // Input available immediately
           a = getchar();
           //printf("%d\n",c);

           switch(a) {
               /**<  */
           case 27:
               b=getchar();
               c=getchar();
               switch(c) {
                   /**< Arrow Up */
               case 65:
                   xdo_mousemove_relative(p_xt,0,-POS_PLUS);
                   break;
                   /**< Arrow Down */
               case 66:
                   xdo_mousemove_relative(p_xt,0,POS_PLUS);
                   break;
                   /**< Arrow Right */
               case 67:
                   xdo_mousemove_relative(p_xt,POS_PLUS,0);
                   break;
                   /**< Arrow Left */
               case 68:
                   xdo_mousemove_relative(p_xt,-POS_PLUS,0);
                   break;
               };
               break;

                       /**< left click */
           case 'q':
               xdo_click(p_xt,CURRENTWINDOW,1);
               break;
           case 'p':
                       /**< right click */
               xdo_click(p_xt,CURRENTWINDOW,3);
               break;


                       /**< mouse left button down  */
           case 'w':
               xdo_mousedown(p_xt,CURRENTWINDOW,1);
               break;

                       /**< mouse left button up */
           case 'e':
               xdo_mouseup(p_xt,CURRENTWINDOW,1);
               break;

                       /**< for longer distance mouse move */
           case 'y':
               xdo_mousemove_relative(p_xt,0,-D_POS_PLUS);
               break;
           case 'n':
               xdo_mousemove_relative(p_xt,0,D_POS_PLUS);
               break;
           case 'g':
               xdo_mousemove_relative(p_xt,-D_POS_PLUS,0);
               break;
           case 'j':
               xdo_mousemove_relative(p_xt,D_POS_PLUS,0);
               break;

                       /**< Delete key */
                       case 127:

xdo_keysequence(p_xt,CURRENTWINDOW,"BackSpace",12000);
                               break;

                       /**< copy paste and cancel  */
                       case 'c':

xdo_keysequence(p_xt,CURRENTWINDOW,"Control_L+c",12000);
                               break;
                       case 'v':

xdo_keysequence(p_xt,CURRENTWINDOW,"Control_L+v",12000);
                               break;
                       case 'z':

xdo_keysequence(p_xt,CURRENTWINDOW,"Control_L+z",12000);
           }
       }
   }

   xdo_free(p_xt);
}

int main(int argc, char **argv) {

   if(argc<=1) {
       printf("rmctool needs a DISPLAY argument\n");
   }
   setTTY();
   nonCanonical(1);
   handlerKeyPress(argv[1]);
   return EXIT_SUCCESS;
}

2011/5/7 Mike Ma <zhtx10在gmail.com>:
> 直接ioctl到/dev里面的鼠标吧。。。
> 貌似这个方法不可行。。
>
> On Wed, 04 May 2011 15:47:39 +0800 microcai <microcai在fedoraproject.org> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> 于 2011年05月04日 04:45, Guannan Ma 写道:
>>> hi everyone
>>>
>>> 最近有这么个想法 只是不知道应该去用什么实现.
>>>
>>> 我的nokia手机可以使用putty for symbian链接我的linux, 我可以在床上的时候控制我的mplayer放音乐.
>>> 问题是putty是command line,没法进行鼠标控制, 我想设计一个小成熟 接受手机putty发送的 移动按钮的事件 来控
>>> 制我的鼠标移动和鼠标左右键的点击.   这样也可以在presentation的时候用手机控制了
>>>
>>> 请知道的朋友告知下 我应该去找寻哪些库文件 哪些函数
>>> 或者给一些例子和网站链接, 中英文都可以
>>>
>>> 我觉得应该是X11 里面的库文件什么的吧?
>>
>> 不用。你只需要一个在 Linux 上运行的使用 /dev/input/uinput 接口的小程序,
>> 然后过网络控制这个程序达到控制 Linux 上鼠标的目的。
>>
>> 不过,建议这种情况下使用 蓝牙。
>>
>>>
>>> Life_is_a_gift在gnma# uname -a
>>> Linux seamless-laptop 2.6.38-9-generic   x86_64
>>> _______________________________________________
>>> Chinese mailing list
>>> Chinese at lists.fedoraproject.org
>>> https://admin.fedoraproject.org/mailman/listinfo/chinese
>>
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v2.0.17 (GNU/Linux)
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>>
>> iQEcBAEBAgAGBQJNwQSaAAoJEKT4Uz7oTANZSwcIALhkXDbG/g8tFw0P9K693XKm
>> Z7HZh7m7RpBSD3nWIBccFk8HweXg+qLpNf+LbE534hp7Ro+6Ajnr6PAkkXHJeIZ+
>> 5h6a/dSMXn+X23Z0FWXWIv0fZLVAqcFcfLo+eOdu+4W81mukBF4MZacGvG2981fc
>> fi9JXNsOgxrWf05ER7HDZ11Bpa4SS6p7BL9JlJ3J305IThqGjm1sDDhClxj4KTFI
>> 94PaixZwI7N3xGZ09Fjs46K/8/qkbm0xBFp95C+Sar9kPMTyhkssr8SVJ1g0RCe3
>> yr8ZxtbdM8r18CmjEH/y0iQCSb5tH5cWi8KMTr/oxdNzWRVBVVv4RNI9roun8QA=
>> =Cred
>> -----END PGP SIGNATURE-----
>> _______________________________________________
>> Chinese mailing list
>> Chinese at lists.fedoraproject.org
>> https://admin.fedoraproject.org/mailman/listinfo/chinese
>
>
> --------------------
> Mike is here.
> My OS:                     Fedora x64
> My blog:                   www.ekd123.org [Powered by Chito]
> My favorite editor:        Emacs 23.2
> My E-mail client:          Mew 6.3 on Emacs 23.2.
> --------------------
> _______________________________________________
> Chinese mailing list
> Chinese at lists.fedoraproject.org
> https://admin.fedoraproject.org/mailman/listinfo/chinese
>


关于邮件列表 Chinese 的更多信息