1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<?php
namespace GameBoy;
class Keyboard
{
public $core;
public $file;
public $keyPressing = null;
public $started = false;
public function __construct(Core $core)
{
$this->core = $core;
exec('stty -icanon -echo');
$this->file = fopen('php://stdin', 'r');
stream_set_blocking($this->file, false);
}
public function check()
{
$key = fread($this->file, 1);
if (!empty($key)) {
$this->keyDown($key);
} elseif (!empty($this->keyPressing)) {
$this->keyUp($this->keyPressing);
}
$this->keyPressing = $key;
}
public function matchKey($key)
{
//Maps a keyboard key to a gameboy key.
//Order: Right, Left, Up, Down, A, B, Select, Start
$keyIndex = array_search($key, Settings::$keyboardButtonMap);
if ($keyIndex === false) {
return -1;
}
return $keyIndex;
}
public function keyDown($key)
{
$keyCode = $this->matchKey($key);
if ($keyCode > -1) {
$this->core->joyPadEvent($keyCode, true);
}
}
public function keyUp($key)
{
$keyCode = $this->matchKey($key);
if ($keyCode > -1) {
$this->core->joyPadEvent($keyCode, false);
}
}
}
|