Skip to content

Python Library Recent

1. Quick Start

Install the package:

bash
pip install imouse-py

2. Basic API Usage

python
import imouse
from imouse.types import MouseSwipeParams

# Connect to the iMouse server (default: localhost)
api = imouse.api(host="localhost")

# Mouse operations
api.mouse_click("FA:9E:10:3A:FE:E8", "", 100, 100)  # Left click at (100, 100)
api.mouse_swipe("FA:9E:10:3A:FE:E8", params=MouseSwipeParams(
    direction='up',
    len=0.9  # Swipe from 10% to 90% of screen
))

Console API

Provides device management and global operations:

  • Device - Device management
  • AirPlay - Screen projection connection
  • USB - iMouse hardware management
  • Group - Group management
  • ImConfig - Global configuration
  • User - Account management
python
import imouse

api = imouse.api(host="localhost")
helper = imouse.helper(api)

console = helper.console
ret = console.device.list_by_id()  # Get all devices
print(ret)

# Cast screen to device
ret = console.airplay.connect('FA:9E:10:3A:FE:E8,FD:9E:10:3A:FE:E0')
if ret:
    print('Success')
else:
    print(f'Failed: {console.error_msg}')

# Disconnect screen
ret = console.airplay.disconnect('FA:9E:10:3A:FE:E8')

# Connect all offline devices
ret = console.airplay.connect_all()

Device API

Control individual devices:

  • Image - Screenshots, image search, OCR
  • KeyBoard - Keyboard operations
  • Mouse - Mouse operations
  • Shortcut - Shortcut commands
python
import imouse
from imouse.utils import file_to_base64
from imouse.types import MouseSwipeParams

api = imouse.api(host="localhost")
helper = imouse.helper(api)

device = helper.device('FA:9E:10:3A:FE:E8')

# Or get all device instances
# device_list = helper.devices()
# device = device_list[0]

# Screenshot
ret = device.image.screenshot()
if ret:
    with open('test.bmp', "wb") as f:
        f.write(ret)

# Find image via OpenCV
img_str = file_to_base64("test1.bmp")
ret = device.image.find_image_cv([img_str])
if len(ret) > 0:
    print(f"Found at [{ret[0].centre[0]},{ret[0].centre[1]}]")

# Swipe up
ret = device.mouse.swipe(MouseSwipeParams(direction='up', len=0.9))

4. Event Handling

python
from typing import List
import imouse
from imouse.api import event
from imouse.models import DeviceInfo, UsbInfo, UserData, ImServerConfigData

@event.on("im_connect")
def im_connect(ver: str):
    print(f"[Event] Connected to kernel: {ver}")

@event.on("dev_connect")
def dev_connect(device_info: DeviceInfo):
    print(f"[Event] Device connected: {device_info.device_id}")

@event.on("dev_disconnect")
def dev_disconnect(device_info: DeviceInfo):
    print(f"[Event] Device disconnected: {device_info.device_id}")

@event.on("dev_change")
def dev_change(device_info: DeviceInfo):
    print(f"[Event] Device changed: {device_info.device_id}")

@event.on("usb_change")
def usb_change(usb_info: UsbInfo):
    print(f"[Event] USB changed: {usb_info}")

@event.on("error_push")
def error_push(message: str, call_fun: str):
    print(f"[Event] Error: {message}, function: {call_fun}")

api = imouse.api(host='192.168.9.9')

5. Configure Logging

python
import logging
from imouse.utils import logger

logger.configure(
    is_debug=True,
    name='imouse',
    log_dir='logs',
    log_level=logging.DEBUG,
    log_show_thread_id=False,
    log_show_file_and_line=False
)

Some3C Phone Farm Documentation