监听鼠标和键盘

如果想监听鼠标和键盘,当然要用到win32的API

python封装的库为pywin32

用这个库可以进行类似点击,敲击键盘的监听,然后记录结果进行分析。

当然也可以用来监听其他人的密码哦


其中pyHook是专门用来监听的一个库

http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=Main_Page


作者给出了使用例子参照如下网址

http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=PyHook_Tutorial


下面将其代码拷贝如下

监听鼠标

import pythoncom, pyHook 

def OnMouseEvent(event):
    # called when mouse events are received
    print 'MessageName:',event.MessageName
    print 'Message:',event.Message
    print 'Time:',event.Time
    print 'Window:',event.Window
    print 'WindowName:',event.WindowName
    print 'Position:',event.Position
    print 'Wheel:',event.Wheel
    print 'Injected:',event.Injected
    print '---'

# return True to pass the event to other handlers
    return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.MouseAll = OnMouseEvent
# set the hook
hm.HookMouse()
# wait forever
pythoncom.PumpMessages()


监听键盘

import pythoncom, pyHook 
 
def OnKeyboardEvent(event):
    print 'MessageName:',event.MessageName
    print 'Message:',event.Message
    print 'Time:',event.Time
    print 'Window:',event.Window
    print 'WindowName:',event.WindowName
    print 'Ascii:', event.Ascii, chr(event.Ascii)
    print 'Key:', event.Key
    print 'KeyID:', event.KeyID
    print 'ScanCode:', event.ScanCode
    print 'Extended:', event.Extended
    print 'Injected:', event.Injected
    print 'Alt', event.Alt
    print 'Transition', event.Transition
    print '---'
 
# return True to pass the event to other handlers
    return True
 
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

《完》

以下是Java代码示例,展示如何监听鼠标键盘的操作,并将其存入集合中: ```java import java.awt.AWTException; import java.awt.MouseInfo; import java.awt.Point; import java.awt.Robot; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.List; public class MouseAndKeyboardListener implements MouseListener, KeyListener { private Robot robot; private List<String> actions; public MouseAndKeyboardListener() { try { robot = new Robot(); } catch (AWTException e) { e.printStackTrace(); } actions = new ArrayList<>(); } @Override public void mouseClicked(MouseEvent e) { Point mousePoint = MouseInfo.getPointerInfo().getLocation(); actions.add("Mouse clicked at (" + mousePoint.x + ", " + mousePoint.y + ")"); } @Override public void mousePressed(MouseEvent e) { Point mousePoint = MouseInfo.getPointerInfo().getLocation(); actions.add("Mouse pressed at (" + mousePoint.x + ", " + mousePoint.y + ")"); } @Override public void mouseReleased(MouseEvent e) { Point mousePoint = MouseInfo.getPointerInfo().getLocation(); actions.add("Mouse released at (" + mousePoint.x + ", " + mousePoint.y + ")"); } @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { actions.add("Key pressed: " + KeyEvent.getKeyText(e.getKeyCode())); } @Override public void keyReleased(KeyEvent e) {} public List<String> getActions() { return actions; } } ``` 在这个示例中,我们使用了Java的AWT库中的Robot类来模拟鼠标键盘的操作。我们实现了MouseListener和KeyListener接口,以便监听这些操作。每次操作发生时,我们都会将其转换为一个字符串,并将其存入一个List集合中。最后,我们可以通过调用`getActions()`方法来获取所有已记录的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值