/*
*
* Copyright (c) 2002 Steve Slaven, All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "debug.h"
#define IS_CMD( x, y ) strncmp( x, y, strlen( y ) ) == 0
#include "kbd.h"
|
[comment]
|
/* Generates an X event, like keypress/mouseclick/move/etc
like a little man in your computer. :) */
|
|
KeyCode thing_to_keycode( Display *d, char *thing )
|
{
KeyCode kc;
KeySym ks;
ks = XStringToKeysym( thing );
if( ks == NoSymbol ){
fprintf( stderr, "Unable to resolve keysym for '%s'/n", thing );
return( thing_to_keycode( d, "space" ) );
}
kc = XKeysymToKeycode( d, ks );
dmsg( 1, "String '%s' maps to keysym '%d'/n", thing, ks );
dmsg( 1, "String '%s' maps to keycode '%d'/n", thing, kc );
return( kc );
}
|
void send_key( Display *d, char *thing )
|
{
int probidx;
char *wrap_key = NULL;
dmsg( 1, "Sending key '%s'/n", thing );
/* Catch some common problem characters (thanks Martin Pirker) */
probidx = 0;
while( problems[ probidx ] != NULL ) {
if( strcmp( thing, problems[ probidx ] ) == 0 ) {
wrap_key = problems[ probidx + 1 ];
thing = problems[ probidx + 2 ];
}
probidx += 3;
}
if( wrap_key != NULL )
XTestFakeKeyEvent( d, thing_to_keycode( d, wrap_key ), True, CurrentTime );
XTestFakeKeyEvent( d, thing_to_keycode( d, thing ), True, CurrentTime );
XTestFakeKeyEvent( d, thing_to_keycode( d, thing ), False, CurrentTime );
if( wrap_key != NULL )
XTestFakeKeyEvent( d, thing_to_keycode( d, wrap_key ), False, CurrentTime );
}
|
void mouse_click( Display *d, int button )
|
{
dmsg( 1, "Clicking mouse button %d/n", button );
XTestFakeButtonEvent( d, button, True, CurrentTime );
XTestFakeButtonEvent( d, button, False, CurrentTime );
}
|
void mouse_move( Display *d, int x, int y )
|
{
dmsg( 1, "Moving mouse to %d,%d/n", x, y );
XTestFakeMotionEvent( d, -1, x, y, CurrentTime );
}
|
void mouse_rel_move( Display *d, int x, int y )
|
{
dmsg( 1, "Moving mouse relatively by %d,%d/n", x, y );
/* This does not match my docs... hrm... */
XTestFakeRelativeMotionEvent( d, x, y, CurrentTime );
/* Should include screen?
XTestFakeRelativeMotionEvent( d, -1, x, y, CurrentTime );
*/
}
|
void process_command( Display *d, const char *cmd )
|
{
/* Process a command */
int tmpx,tmpy;
char str[ 128 ];
bzero( str, 128 );
if( IS_CMD( cmd, "mouseclick " ) ) {
sscanf( cmd, "mouseclick %d", &tmpx );
mouse_click( d, tmpx );
}else if( IS_CMD( cmd, "key " ) ) {
strncpy( str, &cmd[ 4 ], 128 );
send_key( d, str );
}else if( IS_CMD( cmd, "keydown " ) ) {
strncpy( str, &cmd[ 8 ], 128 );
XTestFakeKeyEvent( d, thing_to_keycode( d, str ), True, CurrentTime );
}else if( IS_CMD( cmd, "keyup " ) ) {
strncpy( str, &cmd[ 6 ], 128 );
XTestFakeKeyEvent( d, thing_to_keycode( d, str ), False, CurrentTime );
}else if( IS_CMD( cmd, "mousemove " ) ) {
sscanf( cmd, "mousemove %d %d", &tmpx, &tmpy );
mouse_move( d, tmpx, tmpy );
}else if( IS_CMD( cmd, "mousermove " ) ) {
sscanf( cmd, "mousermove %d %d", &tmpx, &tmpy );
mouse_rel_move( d, tmpx, tmpy );
}else if( IS_CMD( cmd, "sleep " ) ) {
sscanf( cmd, "sleep %d", &tmpx );
dmsg( 1, "sleep %d/n", tmpx );
sleep( tmpx );
}else if( IS_CMD( cmd, "usleep " ) ) {
sscanf( cmd, "usleep %d", &tmpx );
dmsg( 1, "usleep %d/n", tmpx );
usleep( tmpx );
}else if( IS_CMD( cmd, "mousedown " ) ) {
sscanf( cmd, "mousedown %d", &tmpx );
XTestFakeButtonEvent( d, tmpx, True, CurrentTime );
}else if( IS_CMD( cmd, "mouseup " ) ) {
sscanf( cmd, "mouseup %d", &tmpx );
XTestFakeButtonEvent( d, tmpx, False, CurrentTime );
}else if( IS_CMD( cmd, "str " ) ) {
cmd += 4;
while( cmd[ 0 ] != 0 ) {
str[ 0 ] = cmd[ 0 ];
send_key( d, str );
cmd++;
}
}else{
fprintf( stderr, "Unknown command '%s'/n", cmd );
}
XFlush( d );
}
|
int main( int argc, char *argv[] )
|
{
Display *dpy = NULL;
int cnt, tmp_i;
char *buf, *display = NULL;
int opt;
while( ( opt = getopt( argc, argv, "hd:x:" ) ) != EOF ) {
switch( opt ) {
case 'h':
printf( "xte v" VERSION "/n"
"Generates fake input using the XTest extension, more reliable than xse/n"
"Author: Steve Slaven - http://hoopajoo.net/n"
"Current keyboard map: " KBDMAP "/n"
"/n"
"usage: %s [-h] [-x display] [arg ..]/n"
"/n"
" -h this help/n"
" -x send commands to remote X server. Note that some commands/n"
" may not work correctly unless the display is on the console,/n"
" e.g. the display is currently controlled by the keyboard and/n"
" mouse and not in the background. This seems to be a limitation/n"
" of the XTest extension./n"
" arg args instructing the little man on what to do (see below)/n"
" if no args are passed, commands are read from stdin separated/n"
" by newlines, to allow a batch mode/n"
"/n"
" Commands:/n"
" key k Press and release key k/n"
" keydown k Press key k down/n"
" keyup k Release key k/n"
" str string Do a bunch of key X events for each char in string/n"
" mouseclick i Click mouse button i/n"
" mousemove x y Move mouse to screen position x,y/n"
" mousermove x y Move mouse relative from current location by x,y/n"
" mousedown i Press mouse button i down/n"
" mouseup i Release mouse button i/n"
" sleep x Sleep x seconds/n"
" usleep x uSleep x microseconds/n"
"/n"
"Some useful keys (case sensitive)/n"
" Home/n"
" Left/n"
" Up/n"
" Right/n"
" Down/n"
" Page_Up/n"
" Page_Down/n"
" End/n"
" Return/n"
" Backspace/n"
" Tab/n"
" Escape/n"
" Delete/n"
" Shift_L/n"
" Shift_R/n"
" Control_L/n"
" Control_R/n"
" Meta_L/n"
" Meta_R/n"
" Alt_L/n"
" Alt_R/n"
"/n"
"Sample, drag from 100,100 to 200,200 using mouse1:/n"
" xte 'mousemove 100 100' 'mousedown 1' 'mousemove 200 200' 'mouseup 1'/n"
"/n"
, argv[ 0 ] );
exit( 0 );
break;
case 'd':
sscanf( optarg, "%d", &tmp_i );
dmsg( 2, "Debug set to %d/n", tmp_i );
debug_level( tmp_i );
break;
case 'x':
display = optarg;
break;
case '?':
fprintf( stderr, "Unknown option '%c'/n", optopt );
break;
default:
fprintf( stderr, "Unhandled option '%c'/n", opt );
break;
}
}
dpy = XOpenDisplay( display );
if( dpy == NULL ) {
fprintf( stderr, "Unable to open display '%s'/n", display == NULL ? "default" : display );
exit( 1 );
}
if( argc - optind >= 1 ) {
/* Arg mode */
for( cnt = optind; cnt < argc; cnt++ ) {
process_command( dpy, argv[ cnt ] );
}
}else{
/* STDIN mode */
buf = (char *)malloc( 128 );
while( fgets( buf, 128, stdin ) ) {
buf[ strlen( buf ) - 1 ] = 0; /* Chop /n */
process_command( dpy, buf );
}
}
XCloseDisplay( dpy );
exit( 0 );
}
|
分享到:
相关推荐
MFC 模拟鼠标按键程序,简单易懂,新手必备!
delphi模拟按键精灵鼠标、键盘操作 让Webbrowser响应回车事件! delphi 模拟鼠标点击动作及模拟输入文字
c#开发的模拟按键,类似按键精灵的简单代码,可以直接运行winform程序,具有一定的参考价值
用两个按键分别模拟鼠标双击和单击的点击操作源码
1.找棋盘数据 棋盘左上角第一个棋子,ce搜索,因为不知道他的值,所以不能精确查找,但是单击棋子后,数据会变化所以找到棋子数据。查看什么访问量它,找到访问它...鼠标模拟的坐标和数组坐标是反的,注意坐标的判断。
java按键精灵基础代码,包括模拟操作鼠标,模拟键盘输入的基础功能,和一个游戏示例代码。
商业编程-源码-用应用程序模拟键盘和鼠标按键源代码.zip
这是我写的一个VBS控制鼠标和按键的脚本,供网友参考
介绍了winform模拟鼠标按键的具体实现,有需要的朋友可以参考一下
现在人力成本很贵,matlab很强大,通过matlab模拟人的动作实现自动化处理一些比较规律的事情,是王权可以做到全自动化的,这里提供完全模拟鼠标点击、移动的代码,包括左键、右键、中间按键,可以通过屏幕像素精确...
主要的函数都放在KeyB_Mouse_sub模块中。代码无加密 2.6版更新:利用钩子改进了抓点方式(感谢小FISH给的范例),...里面值得参考的代码:VBA如何利用WIN32 API控制键盘鼠标 VBA利用钩子(勾子)实现鼠标抓点功能等等
学习笔记,osg模型响应键盘按键和鼠标按键,当按下键盘上左键和右键时分别显示模型1和模型2
PostMessage 后台模拟按键 MOUSE VC原码 PostMessage 后台模拟按键 MOUSE VC原码 可以后台操作的,模拟按键,VC 代码.
用键盘模拟鼠标,但当鼠标出问题时,可以实用这个程序! 一个简单的键盘模拟程序! 此程序没有显示界面,后台运行,没有结束快捷键(使用的鼠标有问题,此程序代替鼠标),只能使用任务管理器结束! F12是启动鼠标...
Delphi 模拟按键,将虚拟键转换为扫描码,模拟鼠标右键和左键以及发送消息的功能,Shift和Ctrl都定义了特殊的功能,一个简单的键盘鼠标模拟程序,适合Delphi初学者。比如模拟点击鼠标左键,具体的代码编写为: ...
java 模拟键盘鼠标操作批量登录qq,代码仅供学习参考使用
C#模拟键盘鼠标操作外部应用程式、源代码、项目集、代码下载可直接使用
这是一个开发包,程序很小,只进行了一次鼠标右键单击的消息,读者可以很容易的了解程序模拟键鼠的相关代码
在Windows大行其道的今天,windows界面程序受到广大用户的欢迎。对这些程序的操作不外乎两种,键盘输入控制和鼠标输入控制。有时,对于繁杂的,或重复...关键字:keybd_event,mouse_event,用应用程序模拟键盘和鼠标按键