Skip to content

Commit

Permalink
20240722
Browse files Browse the repository at this point in the history
更新了鼠标平滑移动
  • Loading branch information
KingBes committed Jul 22, 2024
1 parent f43a92c commit 6396801
Show file tree
Hide file tree
Showing 16 changed files with 1,041 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ public function mouse_move_mouse(int $x, int $y): self
*/
public function move_mouse_rel(int $offset_x, int $offset_y): self
{}

/**
* 鼠标平滑移动指定位置 function
*
* @param integer $x
* @param integer $y
* @param integer $duration_ms 毫秒
* @param string $tween 预览补间 参数请看下面 `预览补间` 说明
* @return self
*/
public function move_mouse_smooth(int $x, int $y, int $duration_ms, string $tween): self
{}

/**
* 当前平滑移动鼠标 function
*
* @param integer $offset_x
* @param integer $offset_y
* @param integer $duration_ms 毫秒
* @param string $tween 预览补间 参数请看下面 `预览补间` 说明
* @return self
*/
public function move_mouse_smooth_rel(int $offset_x, int $offset_y, int $duration_ms, string $tween): self
{}

```

## 屏幕
Expand Down Expand Up @@ -190,6 +215,43 @@ sleep(5); //延迟5秒
$Keyboard->onClickKey(65)
```

# 预览补间 `可用的鼠标补间`

你可以在这里预览补间: https://easings.net/

- `linear`
- `ease_in_quad`
- `ease_out_quad`
- `ease_in_out_quad`
- `ease_in_cubic`
- `ease_out_cubic`
- `ease_in_out_cubic`
- `ease_in_quart`
- `ease_out_quart`
- `ease_in_out_quart`
- `ease_in_quint`
- `ease_out_quint`
- `ease_in_out_quint`
- `ease_in_sine`
- `ease_out_sine`
- `ease_in_out_sine`
- `ease_in_expo`
- `ease_out_expo`
- `ease_in_out_expo`
- `ease_in_circ`
- `ease_out_circ`
- `ease_in_out_circ`
- `ease_in_elastic`
- `ease_out_elastic`
- `ease_in_out_elastic`
- `ease_in_back`
- `ease_out_back`
- `ease_in_out_back`
- `ease_in_bounce`
- `ease_out_bounce`
- `ease_in_out_bounce`


# 键盘的整数键码值

在 Windows 操作系统中,键盘上的一些按键对应着特定的整数键码值(keyCode)。以下是一些常见的 win 键盘按键及其对应的十进制数字:
Expand Down
1 change: 1 addition & 0 deletions build/Robot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
robot.dll
185 changes: 185 additions & 0 deletions build/Robot/robot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

declare(strict_types=1);

// use FFI;

class Robot
{
public FFI $ffi;

public function __construct()
{
if (PHP_OS_FAMILY != "Windows") {
throw new Exception("The OS must be windows!");
}
$header = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . "robot_php.h");
$this->ffi = FFI::cdef($header, "robot.dll");
}
}

/**
* 鼠标 class
*/
class Mouse extends Robot
{
/**
* 鼠标指针位置 function
*
* @return array
*/
public function mouse_pos(): array
{
$pos = $this->ffi->mouse_pos();
return ["x" => $pos->x, "y" => $pos->y];
}

/**
* 鼠标单击 function
*
* @param string $button left right middle
* @return self
*/
public function mouse_click(string $button): self
{
$this->ffi->mouse_click($button);
return $this;
}

/**
* 鼠标双击 function
*
* @param string $button left right middle
* @return self
*/
public function mouse_double_click(string $button): self
{
$this->ffi->mouse_double_click($button);
return $this;
}

/**
* 将鼠标(左)拖动到指定位置。 function
*
* @param integer $x
* @param integer $y
* @return self
*/
public function mouse_left_drag(int $x, int $y): self
{
$this->ffi->mouse_left_drag($x, $y);
return $this;
}

/**
* 相对于当前位置拖动鼠标(左)。 function
*
* @param integer $offset_x
* @param integer $offset_y
* @return self
*/
public function mouse_drag_rel(int $offset_x, int $offset_y): self
{
$this->ffi->mouse_drag_rel($offset_x, $offset_y);
return $this;
}

/**
* 移动鼠标到 x y function
*
* @param integer $x
* @param integer $y
* @return self
*/
public function mouse_move_mouse(int $x, int $y): self
{
$this->ffi->mouse_move_mouse($x, $y);
return $this;
}

/**
* 相对于当前位置移动鼠标。 function
*
* @param integer $offset_x
* @param integer $offset_y
* @return self
*/
public function move_mouse_rel(int $offset_x, int $offset_y): self
{
$this->ffi->move_mouse_rel($offset_x, $offset_y);
return $this;
}

public function move_mouse_smooth(int $x, int $y, int $duration_ms, string $tween): self
{
$start_pos = $this->ffi->mouse_pos();
$dist_x = $x - $start_pos->x;
$dist_y = $y - $start_pos->y;
$steps = intval(max(50, ($duration_ms) / 10));
$dt = intval(($duration_ms) / $steps);
$i = 0;
$factor = 0.0;
while ($i < $steps) {
$factor = $this->ffi->factor($tween, $i, $steps);
$current_x = intval($start_pos->x + $factor * $dist_x);
$current_y = intval($start_pos->y + $factor * $dist_y);
$this->ffi->move_mouse($current_x, $current_y);
usleep($dt);
$i++;
}

return $this;
}

/* public function normalize_php(int $i, int $s): float
{
return $this->ffi->normalize_php($i, $s);
}
public function tween(string $t, float $n): float
{
return $this->ffi->tween($t, $n);
} */
}

/**
* 屏幕 class
*/
class Screen extends Robot
{
/**
* 获取屏幕指定位置的颜色rgb function
*
* @param integer $x
* @param integer $y
* @return array
*/
public function pixel_color(int $x, int $y): array
{
$rgb = $this->ffi->pixel_color($x, $y);
return [
"r" => $rgb->r,
"g" => $rgb->g,
"b" => $rgb->b
];
}

/**
* 获取屏幕大小 function
*
* @return array
*/
public function screen_size(): array
{
$size = $this->ffi->screen_size();
return [
"width" => $size->width,
"height" => $size->height
];
}
}

$Mouse = new Mouse();
$size = $Mouse->move_mouse_smooth(0, 0, 3000, "ease_in_out_bounce");

// var_dump($size);
Loading

0 comments on commit 6396801

Please sign in to comment.