Add ingame keypressed event in player category

This commit is contained in:
Scratch-hv | Oeil-de-Lynx 2024-04-16 15:25:35 +00:00
commit edfe80f357
2 changed files with 84 additions and 1 deletions

View file

@ -153,13 +153,14 @@
</category>
<sep></sep>
<category name="Player" colour="#5468ff">
<block type="player_username"/>
<block type="player_ingame"/>
<sep gap="48"></sep>
<block type="player_position"/>
<block type="player_motion"/>
<block type="player_indimension"/>
<sep gap="48"></sep>
<block type="player_username"/>
<block type="player_keypressed"/>
</category>
<category name="Display" colour="#f73030">
<block type="display_displaychattext"/>

View file

@ -85,6 +85,88 @@ function register() {
}, (block) => {
return [`ModAPI.getProfileName()`, javascriptGenerator.ORDER_ATOMIC];
})
registerBlock(`${categoryPrefix}keypressed`, {
message0: 'when in game key %1 pressed do %2 %3',
args0: [
{
"type": "field_dropdown",
"name": "MENU3",
"options": keyBoard
},
{
"type": "input_dummy"
},
{
"type": "input_statement",
"name": "BLOCKS"
}
],
inputsInline: true,
colour: categoryColor,
}, (block) => {
const MENU3 = block.getFieldValue('MENU3');
const BLOCKS = javascriptGenerator.statementToCode(block, 'BLOCKS');
const code = `ModAPI.addEventListener("key", function(ev){
if(ev.key == ${MENU3}){${BLOCKS}}});`;
return `${code}\n`;
})
}
const keyBoard = [
["SPACE", "57"],
["UP ARROW", "200"],
["UP DOWN", "208"],
["UP LEFT", "203"],
["UP RIGHT", "205"],
["A", "30"],
["B", "48"],
["C", "46"],
["D", "32"],
["E", "18"],
["F", "33"],
["G", "34"],
["H", "35"],
["I", "23"],
["J", "36"],
["K", "37"],
["L", "38"],
["M", "50"],
["N", "49"],
["O", "24"],
["P", "25"],
["Q", "16"],
["R", "19"],
["S", "31"],
["T", "20"],
["U", "22"],
["V", "47"],
["W", "17"],
["X", "45"],
["Y", "21"],
["Z", "44"],
["0", "11"],
["1", "2"],
["2", "3"],
["3", "4"],
["4", "5"],
["5", "6"],
["6", "7"],
["7", "8"],
["8", "9"],
["9", "10"],
["F1", "59"],
["F2", "60"],
["F3", "61"],
["F4", "62"],
["F5", "63"],
["F6", "64"],
["F7", "65"],
["F8", "66"],
["F9", "67"],
["F10", "68"],
["F11", "87"],
["F12", "88"],
];
export default register;