Skip to content

Commit

Permalink
20240808
Browse files Browse the repository at this point in the history
更新按下和弹起(键盘)(鼠标)
  • Loading branch information
KingBes committed Aug 8, 2024
1 parent 8926e5e commit 4c3ebe9
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 35 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ public function move_mouse_smooth(int $x, int $y, int $duration_ms, string $twee
public function move_mouse_smooth_rel(int $offset_x, int $offset_y, int $duration_ms, string $tween): self
{}

/**
* 鼠标按下 function
*
* @param string $button left right middle
* @return self
*/
public function mouse_down(string $button): self
{}

/**
* 鼠标弹起 function
*
* @param string $button left right middle
* @return self
*/
public function mouse_up(string $button): self
{}
```

## 屏幕
Expand Down Expand Up @@ -171,6 +188,24 @@ public function isKeyPressed(int $key): bool
*/
public function onClickKey(int $key): void
{}

/**
* 按下键盘某键 function
*
* @param integer $key
* @return self
*/
public function pressKey(int $key): self
{}

/**
* 弹起键盘某键 function
*
* @param integer $key
* @return self
*/
public function releaseKey(int $key): self
{}
```


Expand All @@ -188,6 +223,21 @@ $pos = $Mouse->mouse_pos();
var_dump($pos);
```

# 实例一点二 `鼠标按下和弹起`

```php
// 引入
use KingBes\PhpRobot\Mouse;

sleep(3);// 等待
// 按下 左键
$Mouse->mouse_down("left");

sleep(3);// 等待
// 弹起 左键
$Mouse->mouse_up("left");
```

# 实例二 `监听键盘A键`

```php
Expand Down Expand Up @@ -215,6 +265,22 @@ sleep(5); //延迟5秒
$Keyboard->onClickKey(65)
```

# 实例三点二 `按下和弹起键盘某键`

```php
use KingBes\PhpRobot\Keyboard;

$Keyboard = new Keyboard;

sleep(3);// 等待
// 按下
$Keyboard->pressKey(65);

sleep(3);// 等待
// 弹起
$Keyboard->releaseKey(65);
```

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

你可以在这里预览补间: https://easings.net/
Expand Down
18 changes: 12 additions & 6 deletions build/Robot/robot.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,17 @@ public function move_mouse_smooth(int $x, int $y, int $duration_ms, string $twee
return $this;
}

/* public function normalize_php(int $i, int $s): float
public function mouse_down(string $button): self
{
return $this->ffi->normalize_php($i, $s);
$this->ffi->mouse_down($button);
return $this;
}

public function tween(string $t, float $n): float
public function mouse_up(string $button): self
{
return $this->ffi->tween($t, $n);
} */
$this->ffi->mouse_up($button);
return $this;
}
}

/**
Expand Down Expand Up @@ -180,6 +182,10 @@ public function screen_size(): array
}

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

sleep(3);
$Mouse->mouse_up("left");

// var_dump($size);
38 changes: 15 additions & 23 deletions build/Robot/robot.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module main

import vrobot
import keys
import time

struct Pos {
Expand Down Expand Up @@ -127,31 +126,24 @@ fn factor(tween &char, i int,steps int) f64 {
'ease_in_out_bounce' { vrobot.ease_in_out_bounce(n) }
else { panic('Tween not found') }
}
// println("m:${m}")
// TODO there must be better way to do this
return m
}

/* fn move_mouse_smooth(x int, y int, duration_ms int)
// 按下鼠标
@[export: 'mouse_down']
fn mouse_down(button &char)
{
start_pos := mouse_pos()
dist_x := f64(x) - start_pos.x
dist_y := f64(y) - start_pos.y
// dist := math.sqrt(math.pow(x - start_pos.x, 2) + math.pow(y - start_pos.y, 2))
steps := int(math.max(50.0, duration_ms * 1000 / 5))
dt := int(f64(duration_ms * 1000) / f64(steps))
} */
vrobot.mouse_down(unsafe { button.vstring() })
}

fn main() {
vrobot.move_mouse_smooth(0, 0, 3000, 'linear')
/* for {
onkey := keys.is_key_pressed(65)
if onkey > 0 {
println('A key')
} else {
println('not A key')
}
time.sleep(1000 * time.millisecond)
} */
// 弹起鼠标
@[export: 'mouse_up']
fn mouse_up(button &char)
{
vrobot.mouse_up(unsafe { button.vstring() })
}

/* fn main() {
vrobot.mouse_down('left')
vrobot.mouse_up('left')
} */
2 changes: 2 additions & 0 deletions build/Robot/robot_php.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ void mouse_left_drag(int x, int y);
void mouse_drag_rel(int offset_x, int offset_y);
void mouse_move_mouse(int x, int y);
void move_mouse_rel(int offset_x, int offset_y);
void mouse_down(const char *button);
void mouse_up(const char *button);

void move_mouse(int x, int y);
double factor(const char *tween, int i, int steps);
Expand Down
4 changes: 2 additions & 2 deletions build/Robot/vrobot/mouse.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn send_mouse_event(x int) {
C.mouse_event(x, 0, 0, 0, 0)
}

fn mouse_down(button string) {
pub fn mouse_down(button string) {
match button {
'left' { send_mouse_event(C.MOUSEEVENTF_LEFTDOWN) }
'right' { send_mouse_event(C.MOUSEEVENTF_RIGHTDOWN) }
Expand All @@ -18,7 +18,7 @@ fn mouse_down(button string) {
}
}

fn mouse_up(button string) {
pub fn mouse_up(button string) {
match button {
'left' { send_mouse_event(C.MOUSEEVENTF_LEFTUP) }
'right' { send_mouse_event(C.MOUSEEVENTF_RIGHTUP) }
Expand Down
34 changes: 34 additions & 0 deletions build/keyboard/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ extern "C"
KEYBOARD_API bool isKeyPressed(int key);
// 默认点击了某按键
KEYBOARD_API void simulateKeyPress(int keyCode);
// 按下某按键
KEYBOARD_API void pressKey(int keyCode);
// 弹起某按键
KEYBOARD_API void releaseKey(int keyCode);
}
#endif

Expand All @@ -38,6 +42,36 @@ void simulateKeyPress(int keyCode)
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(INPUT));

input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
}

// 新增:按下某键
void pressKey(int keyCode)
{
WORD wKeyCode = static_cast<WORD>(keyCode);
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wScan = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;

input.ki.wVk = wKeyCode;
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(INPUT));
}

// 新增:弹起某键
void releaseKey(int keyCode)
{
WORD wKeyCode = static_cast<WORD>(keyCode);
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wScan = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;

input.ki.wVk = wKeyCode;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
}
20 changes: 18 additions & 2 deletions build/keyboard/keyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,33 @@ public function onClickKey(int $key): void
{
$this->ffi->simulateKeyPress($key);
}

public function pressKey(int $key): self
{
$this->ffi->pressKey($key);
return $this;
}

public function releaseKey(int $key): self
{
$this->ffi->releaseKey($key);
return $this;
}
}

$Keyboard = new Keyboard;

sleep(3);
$Keyboard->pressKey(65);
sleep(3);
$Keyboard->releaseKey(65);
/* while (true) {
if ($Keyboard->isKeyPressed(65)) {
echo "'A'";
}
usleep(100);
} */

sleep(5);
/* sleep(5);
$Keyboard->onClickKey(65);
$Keyboard->onClickKey(65); */
4 changes: 3 additions & 1 deletion build/keyboard/keyboard_php.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bool isKeyPressed(int key);
void simulateKeyPress(int keyCode);
void simulateKeyPress(int keyCode);
void pressKey(int keyCode);
void releaseKey(int keyCode);
24 changes: 24 additions & 0 deletions src/KeyBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ public function onClickKey(int $key): void
{
$this->ffi->simulateKeyPress($key);
}

/**
* 按下键盘某键 function
*
* @param integer $key
* @return self
*/
public function pressKey(int $key): self
{
$this->ffi->pressKey($key);
return $this;
}

/**
* 弹起键盘某键 function
*
* @param integer $key
* @return self
*/
public function releaseKey(int $key): self
{
$this->ffi->releaseKey($key);
return $this;
}
}
24 changes: 24 additions & 0 deletions src/Mouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,28 @@ public function move_mouse_smooth_rel(int $offset_x, int $offset_y, int $duratio
$this->move_mouse_smooth($dest_x, $dest_y, $duration_ms, $tween);
return $this;
}

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

/**
* 鼠标弹起 function
*
* @param string $button left right middle
* @return self
*/
public function mouse_up(string $button): self
{
$this->ffi->mouse_up($button);
return $this;
}
}
Binary file modified src/keyboard.dll
Binary file not shown.
4 changes: 3 additions & 1 deletion src/keyboard_php.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bool isKeyPressed(int key);
void simulateKeyPress(int keyCode);
void simulateKeyPress(int keyCode);
void pressKey(int keyCode);
void releaseKey(int keyCode);
Binary file modified src/robot.dll
Binary file not shown.
2 changes: 2 additions & 0 deletions src/robot_php.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ void mouse_left_drag(int x, int y);
void mouse_drag_rel(int offset_x, int offset_y);
void mouse_move_mouse(int x, int y);
void move_mouse_rel(int offset_x, int offset_y);
void mouse_down(const char *button);
void mouse_up(const char *button);

void move_mouse(int x, int y);
double factor(const char *tween, int i, int steps);
Expand Down

0 comments on commit 4c3ebe9

Please sign in to comment.