免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 21388 | 回复: 17
打印 上一主题 下一主题

如何控制鼠标和键盘? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-18 15:20 |只看该作者 |倒序浏览
python在windows下有什么模块可以控制鼠标和键盘吗?
想写个程序自动控制鼠标和键盘完成任务
谢谢

论坛徽章:
0
2 [报告]
发表于 2007-04-18 15:28 |只看该作者
找到了...

  1. >>> from ctypes import *
  2. >>> windll.user32.SetCursorPos(100, 100)
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2007-04-19 10:07 |只看该作者
楼上搞定了没?

论坛徽章:
0
4 [报告]
发表于 2007-04-19 10:39 |只看该作者
搞定了几个东西
1,取得鼠标位置,get_mpos()
2,移动鼠标,set_mpos()
3,鼠标左键点击,move_click()
4,键盘输入(部分搞定,那个scancode还不明白是啥意思,为什么0x99输出的是字符'p'???)
代码如下

  1. from ctypes import *
  2. import time

  3. #<some pos>
  4. win_start = (9, 753)
  5. close_window = (1017, 4)
  6. #</some pos>

  7. PUL = POINTER(c_ulong)
  8. class KeyBdInput(Structure):
  9.         _fields_ = [("wVk", c_ushort),("wScan", c_ushort),("dwFlags", c_ulong),("time", c_ulong),("dwExtraInfo", PUL)]

  10. class HardwareInput(Structure):
  11.         _fields_ = [("uMsg", c_ulong),("wParamL", c_short),("wParamH", c_ushort)]

  12. class MouseInput(Structure):
  13.         _fields_ = [("dx", c_long),("dy", c_long),("mouseData", c_ulong),("dwFlags", c_ulong),("time",c_ulong),("dwExtraInfo", PUL)]

  14. class Input_I(Union):
  15.         _fields_ = [("ki", KeyBdInput),("mi", MouseInput),("hi", HardwareInput)]

  16. class Input(Structure):
  17.         _fields_ = [("type", c_ulong),("ii", Input_I)]

  18. class POINT(Structure):
  19.         _fields_ = [("x", c_ulong),("y", c_ulong)]

  20. #<Get Pos>
  21. def get_mpos():
  22.         orig = POINT()
  23.         windll.user32.GetCursorPos(byref(orig))
  24.         return int(orig.x), int(orig.y)
  25. #</Get Pos>

  26. #<Set Pos>
  27. def set_mpos(pos):
  28.         x,y = pos
  29.         windll.user32.SetCursorPos(x, y)
  30. #</Set Pos>

  31. #<move and click>
  32. def move_click(pos, move_back = False):
  33.         origx, origy = get_mpos()
  34.         set_mpos(pos)
  35.         FInputs = Input * 2
  36.         extra = c_ulong(0)
  37.         ii_ = Input_I()
  38.         ii_.mi = MouseInput( 0, 0, 0, 2, 0, pointer(extra) )
  39.         ii2_ = Input_I()
  40.         ii2_.mi = MouseInput( 0, 0, 0, 4, 0, pointer(extra) )
  41.         x = FInputs( ( 0, ii_ ), ( 0, ii2_ ) )
  42.         windll.user32.SendInput(2, pointer(x), sizeof(x[0]))
  43.         if move_back:
  44.                 set_mpos((origx, origy))
  45.         return origx, origy
  46. #</move and click>


  47. def sendkey(scancode, pressed):
  48.         FInputs = Input * 1
  49.         extra = c_ulong(0)
  50.         ii_ = Input_I()
  51.         flag = 0x8 # KEY_SCANCODE
  52.         ii_.ki = KeyBdInput( 0, 0, flag, 0, pointer(extra) )
  53.         InputBox = FInputs( ( 1, ii_ ) )
  54.        
  55.         if scancode == None:
  56.                 return
  57.         InputBox[0].ii.ki.wScan = scancode
  58.         InputBox[0].ii.ki.dwFlags = 0x8  # KEY_SCANCODE  
  59.         if not(pressed):
  60.                 InputBox[0].ii.ki.dwFlags |= 0x2 # released
  61.         windll.user32.SendInput(1, pointer(InputBox), sizeof(InputBox[0]))
  62.         if pressed:
  63.                 print "%x pressed" % scancode
  64.         else:
  65.                 print "%x released" % scancode

  66. if __name__ == '__main__':
  67.         origx, origy = get_mpos()
  68.         print 'Orig Pos:', origx, origy
  69.         #set_mpos(9, 753)
  70.         #move_click(win_start)
  71.         move_click((800,600))
  72.         time.sleep(1)
  73.         sendkey(0x99,1)
  74.         #keyboard_input()
  75.        
  76.         #set_mpos(origx, origy)


复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2007-04-19 10:47 |只看该作者
哦,原来是这个思路啊。不知道 Perl Win32::GuiTest 的那种思路在 Python 中有对应的实现吗?借鉴一下。
看看我每天都要用的一个小脚本(自动开启虚拟机):
  1. use Win32::GuiTest qw(:ALL);

  2. our $timeoutFlag = 0;
  3. $SIG{'__DIE__'} = sub{ print "$_[0]按回车键退出"; <> };

  4. my $VMPath = shift || die "用法:$0 <VirtualMachine Path> <VM Name>\n";
  5. my $VMName = shift || die "用法:$0 <VirtualMachine Path> <VM Name>\n";
  6. my $OpenPietty = shift;

  7. die "文件 $VMPath 不存在。\n" unless -f $VMPath;

  8. `start $VMPath`;

  9. my $desktop = GetDesktopWindow();

  10. my ($win) = WaitWindowLike( $desktop, "$VMName - VMware Workstation", '^VMUIFrame$', undef, undef, 10 );

  11. die "没有找到虚拟机窗口。请检查虚拟机名称是否的确是: [$VMName].\n" unless $win;

  12. SetForegroundWindow($win);
  13. SendKeys( '%mpp' );

  14. sleep(3);

  15. ShowWindow( $win, 0 );

  16. exec('pietty') if $OpenPietty;

  17. exit(0);
复制代码

用法:
  1. D:\MoChou\perl\StartVMware.pl F:\Debian\Debian.vmx Debian yes
复制代码

论坛徽章:
0
6 [报告]
发表于 2007-04-19 10:56 |只看该作者
貌似很好用的样子...
郁闷python咋就没有

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
7 [报告]
发表于 2007-04-19 11:22 |只看该作者
原帖由 bleem1998 于 2007-4-19 10:56 发表
貌似很好用的样子...
郁闷python咋就没有

应该也有吧。
这些都是 windows 提供的功能了。
如果没有的话,那你给写一个不就得了。

论坛徽章:
0
8 [报告]
发表于 2007-04-19 11:32 |只看该作者
键盘的基本搞定了
不过只能输入UNICODE的字符
接下来弄屏幕窗口的
类似WaitWindowLike()和GetDesktopWindow()
干巴爹~~干巴爹~~

论坛徽章:
0
9 [报告]
发表于 2007-04-20 13:29 |只看该作者
有个东西叫AutoIt
用来自动完成工作很不错
脚本还可以编译成exe
放到任何机器上都可以跑
什么都不依赖
貌似不开源

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
10 [报告]
发表于 2007-04-20 13:52 |只看该作者
原帖由 bleem1998 于 2007-4-20 13:29 发表
有个东西叫AutoIt
用来自动完成工作很不错
脚本还可以编译成exe
放到任何机器上都可以跑
什么都不依赖
貌似不开源

WinRun?
呵呵,其实要找开源的还不如用 Perl。
也可以编译。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP