When two keys are pressed simultaneously, a certain function should be launched. How to make a check that two keys are pressed simultaneously in the Corona SDK? I think that when calling the function of one button, we need to check whether the function of the second button is called.

The task itself:

By simultaneously pressing the buttonTop and buttonRight rotate an object by 45 ° using the object:rotate(45) method object:rotate(45)

    1 answer 1

    I did not deal with the Corona SDK, but most likely it is necessary to make a table, where the keys are the ID of the buttons, and the values ​​are boolean. Each button handler sets its button to true . After all the keys have been processed (the update method, maybe) we drive the checks of the combinations, and after that we reset the table.

    PS: there may be native functions / tables for these purposes.

    Solution pseudocode:

     keyhandlers = {} create_keyhandler = function(keyid) return function() keyhandlers[keyid] = true end end update = function() -- This function must be called after all keyhandlers ... -- handle pressed keys if keyhandlers['Top'] and keyhandlers['Right'] then object:rotate(45) end ... -- reset keyhandlers keyhandlers = {} ... end addhandler('buttonTop', create_keyhandler('Top')) -- replace addhandler with real function or something else addhandler('buttonRight', create_keyhandler('Right')) 
    • How about entering a variable with a value of false , and when you click on the button to change it to true ? Then you can check for true in the function of another button. - NekoDev
    • I just offered a universal solution. You can make the function constructor event recorders. Again, all keys are reset in bulk, just table_of_keys = {} . In a short time I will give a pseudo-code solution. - val
    • Again, it is not known in which order the functions will be run, which means duplicating the code. - val