Change Camera with KeyPress

i already have myself a switch camera script, but it works only with a ui button so i would like to know how to do that with a keypress, i do not know how to do that

var SwitchCamera = pc.createScript('switchCamera');

SwitchCamera.attributes.add('firstPCamera', {
    type: 'entity',
    title: 'FirstPCamera'

SwitchCamera.attributes.add('thirdPCamera', {
    type: 'entity',
    title: 'ThirdPCamera'

SwitchCamera.attributes.add('switchButton', {
    type: 'entity',
    title: 'switchButton'

// initialize code called once per entity
SwitchCamera.prototype.initialize = function () {

    this.switchButton.button.on("click", this.onSwitchButtonClick, this);


SwitchCamera.prototype.onSwitchButtonClick = function () {

    this.firstPCamera.enabled = !this.firstPCamera.enabled;
    this.thirdPCamera.enabled = !this.thirdPCamera.enabled;


// update code called every frame
SwitchCamera.prototype.update = function (dt) {


// swap method called for script hot-reloading
// inherit your script state here
// SwitchCamera.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:

thats my already existing script, i just need it to work when pressing the key C

Hey @Deadshot1m24,

You could run the same function - onSwitchButtonClick - when you detect a keypress on C. Refer to the wasPressed documentation here - Basic Keyboard Input | Learn PlayCanvas

but what parts would i change? and what would i change it to :confused:

You would need to listen for a keypress event in your update method the way it has been shown in the documentation. If the C keypress is detected, you can simply run the function you have already written by calling it.

that is not documentation that is a random teacher profile :confused:


My bad. My keyboard has been acting up, I’ve never even seen that profile before :joy:, don’t know how that happened. It’s fixed now. Really sorry for the issue @Deadshot1m24.

thank you