-
Notifications
You must be signed in to change notification settings - Fork 4
/
RS_KeyboardEvent.js
343 lines (331 loc) · 11.6 KB
/
RS_KeyboardEvent.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//================================================================
// RS_KeyboardEvent.js
// ---------------------------------------------------------------
// The MIT License
// Copyright (c) 2017 biud436
// ---------------------------------------------------------------
// Free for commercial and non commercial use.
//================================================================
var Imported = Imported || {};
Imported.RS_KeyboardEvent = true;
/*:ko
* @target MZ
* @plugindesc (v1.01) 수동으로 키보드 이벤트를 만들고 브라우저에 키보드 입력 이벤트를 보냅니다.
* @author biud436
* @url https://biud436.tistory.com
*
* @help
* =============================================================================
* 소개
* ============================================================================= *
* 실제 키를 입력된 것처럼 가상의 키보드 입력 매크로를 만듭니다.
*
* =============================================================================
* 플러그인 커맨드
* =============================================================================
* - 'keyCode' 인자 값은 0x58 또는 80처럼 16진수 또는 10진수 값이여야 합니다.
* - 'keyName' 인자 값은 Input.keyMapper에서 찾을 수없는 문자열 값이어야 합니다.
*
* - 사용자가 Input 클래스의 주요 로직에 가상 키 코드를 보낼 수있게 하는 명령입니다.
* KeyEvent executeString keyName
* KeyEvent executeKey keyCode
*
* - 이 명령은 사용자가 Input 클래스에 새로운 가상 키 코드를 추가 할 수있게합니다.
* KeyEvent addNewKey keyCode keyName
*
* - 예제 코드는 다음과 같습니다.
* KeyEvent executeString left
* KeyEvent executeString escape
* KeyEvent executeKey 0x58
* KeyEvent addNewKey 0x50 p
* =============================================================================
* 스크립트 호출
* =============================================================================
* 두 가지 자바스크립트 함수가 있습니다.
*
* 함수는 Input 클래스의 주요 로직에 가상 키 코드를 보낼 수있게합니다.
*
* Input._makeKeyTiming(keyCode);
*
* 이 함수는 사용자가 Input 클래스에 새로운 가상 키 코드를 추가 할 수있게 합니다.
* Input 클래스의 keyMapper는 JSON 객체이며, 키와 값을 쌍으로 갖습니다.
*
* - 'keyCode' 인자 값은 0x58 또는 80처럼 16진수 또는 10진수 값이여야 합니다.
* - 'keyName' 인자 값은 Input.keyMapper에서 찾을 수없는 문자열 값이어야 합니다.
*
* 성공하면 키 값과 버튼의 이름을 담고 있는 JSON 객체가 콜백 함수에 전달됩니다.
*
* Input._executeJson(keyCode, keyName, func);
*
* - 다음은 예제 코드입니다.
* Input._makeKeyTiming('left');
* Input._makeKeyTiming('escape');
* Input._makeKeyTiming('down');
* Input._makeKeyTiming('control');
* Input._makeKeyTiming(116);
*
*
* 아래 콜백 함수의 첫 번째 인자 값 retObj은 {0x58: 'p'}입니다.
*
* Input._executeJson(0x58, 'p', function (retObj) {
* Object.assign(Input.keyMapper, retObj);
* });
*
* =============================================================================
* Change Log
* =============================================================================
* 2017.01.02 (v1.0.0) - First Release.
* 2017.03.03 (v1.0.1) - Added new function that can add new keyCode.
*
* =============================================================================
* Commands
* =============================================================================
* @command executeString
* @desc
*
* @args keyCode
* @type number
* @desc Specify the key code as you want
* @default 0
*
* @command executeKey
* @desc
*
* @args keyCode
* @type number
* @desc Specify the key code as you want
* @default 0
*
* @command addNewKey
* @desc
*
* @args keyInt
* @type number
* @desc Specify the key code.
* @default 0
*
* @args keyName
* @type string
* @desc Specify the key name.
* @default keyName
*
*/
/*:
* @target MZ
* @plugindesc (v1.01) This plugin allows you to send keyboard input events manually.
* @author biud436
* @url https://biud436.blog.me
* @help
* =============================================================================
* Introduction
* ============================================================================= *
* This plugin can process a keyboard event without pressing a real button within a keyboard.
*
* =============================================================================
* Plugin Commands
* =============================================================================
* - 'keyCode' must be number type like as 0x58 or 80
* - 'keyName' should be the value for a strings that could not found in Input.keyMapper
*
* - These commands allow user to send a virtual key code in main logic of Input class.
* KeyEvent executeString keyName
* KeyEvent executeKey keyCode
*
* - This command allows user to add a new virtual key code in global Input class.
* KeyEvent addNewKey keyCode keyName
*
* - There are example codes at the next line.
* KeyEvent executeString left
* KeyEvent executeString escape
* KeyEvent executeKey 0x58
* KeyEvent addNewKey 0x50 p
* =============================================================================
* Script Calls
* =============================================================================
* Here's two javascript functions.
*
* - This function allows user to send a virtual key code in main logic of Input class.
*
* Input._makeKeyTiming(keyCode);
*
* - This function allows user to add a new virtual key code in global Input class.
* 'keyCode' must be number type like as 0x58 or 80
* 'keyName' should be the value for a strings that could not found in Input.keyMapper
* 'func' object has executed if JSON object is successfully created.
* The first parameter of the callback function is the JSON object that created at the caller.
*
* Input._executeJson(keyCode, keyName, func);
*
* - There are example codes at the next line.
* Input._makeKeyTiming('left');
* Input._makeKeyTiming('escape');
* Input._makeKeyTiming('down');
* Input._makeKeyTiming('control');
* Input._makeKeyTiming(116);
* Input._executeJson(0x58, 'p', function (retObj) {
* Object.assign(Input.keyMapper, retObj);
* });
* =============================================================================
* Change Log
* =============================================================================
* 2017.01.02 (v1.0.0) - First Release.
* 2017.03.03 (v1.0.1) - Added new function that can add new keyCode.
*
* =============================================================================
* Commands
* =============================================================================
* @command executeString
* @desc
*
* @args keyCode
* @type number
* @desc Specify the key code as you want
* @default 0
*
* @command executeKey
* @desc
*
* @args keyCode
* @type number
* @desc Specify the key code as you want
* @default 0
*
* @command addNewKey
* @desc
*
* @args keyInt
* @type number
* @desc Specify the key code.
* @default 0
*
* @args keyName
* @type string
* @desc Specify the key name.
* @default keyName
*/
(function () {
"use strict";
Input._startTime = 0;
Input._makeKeyEvent = function (type, keyCode) {
var isShift = Boolean(keyCode === 0x10);
var isCtrl = Boolean(keyCode === 0x11);
var isAlt = Boolean(keyCode === 0x12);
var evt = new KeyboardEvent(type, {
bubbles: true,
shiftKey: isShift,
ctrlKey: isCtrl,
altKey: isAlt,
});
Object.defineProperty(evt, "keyCode", {
get: function () {
return keyCode;
},
});
document.dispatchEvent(evt);
};
/**
* Swap between key/value
* @param {String}
* @return {Number}
*/
Input._makeVirtualKey = function (keyCode) {
if (typeof keyCode === "string") {
var tempMapper = [];
var vkCode = 0;
var mapper = JsonEx.makeDeepCopy(Input.keyMapper);
var length = Object.keys(mapper).length;
var keys = Object.keys(mapper);
for (var i = 0; i < length; i++) {
tempMapper[mapper[keys[i]]] = keys[i];
}
mapper = null;
vkCode = tempMapper[keyCode];
return vkCode;
}
};
Input._makeKeyTiming = function (keyCode) {
if (typeof keyCode === "string") {
keyCode = this._makeVirtualKey(keyCode);
}
requestAnimationFrame(function (timestamp) {
Input._startTime = timestamp;
var progress = timestamp - Input._startTime;
Input._makeKeyEvent("keydown", keyCode);
if (progress < 2000) {
requestAnimationFrame(function () {
Input._makeKeyEvent("keyup", keyCode);
});
}
});
};
Input._executeJson = function (keyInt, keyName, func) {
var retObj, type, json;
json = {
keyInt: keyName,
};
if (typeof json === "object") retObj = JSON.parse(json);
if (retObj) {
type = Object.keys(retObj);
if (typeof type[0] === "number") {
if (typeof func === "function") func(retObj);
}
}
};
//============================================================================
// Game_Interpreter
//============================================================================
var alias_Game_Interpreter_pluginCommand =
Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
alias_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === "KeyEvent") {
switch (args[0]) {
case "executeString":
var keyCode = Input._makeVirtualKey(args[1]) || 0;
Input._makeKeyTiming(keyCode);
break;
case "executeKey":
var keyCode = parseInt(args[1] || 0);
Input._makeKeyTiming(keyCode);
break;
case "addNewKey":
Input._executeJson(
parseInt(args[1]),
args[2],
function (retObj) {
Object.assign(Input.keyMapper, retObj);
}
);
break;
}
}
};
(() => {
if (Utils.RPGMAKER_NAME === "MZ") {
// 플러그인의 이름을 가져옵니다.
const pluginName = "RS_KeyboardEvent";
const pluginCommands = {
executeString: (args) => {
const keyCode = Input._makeVirtualKey(args.keyCode) || 0;
Input._makeKeyTiming(keyCode);
},
executeKey: (args) => {
const keyCode = parseInt(args.keyCode || 0);
Input._makeKeyTiming(keyCode);
},
addNewKey: (args) => {
Input._executeJson(
parseInt(args.keyInt),
args.keyName,
function (retObj) {
Object.assign(Input.keyMapper, retObj);
}
);
},
};
for (let i in pluginCommands) {
PluginManager.registerCommand(pluginName, i, pluginCommands[i]);
}
}
})();
})();