No way to tell if Ctrl/Shift/Alt keys were pressed with key in Communicator.Operator.Operator onKeyDown

I am working with custom Operator and extending the Communicator.Operator.Operator class from Hoops.

Why does onKeyDown not have any way to see if a modifier key was pressed as well?

In Javascript you can check if a ctrl key was pressed with the event KeyboardEvent: ctrlKey property - Web APIs | MDN

Is this intentional in Hoops or an oversight? Or is there a way for me to easily tell if I did CTRL+Z for example, or CTRL+SHIFT+Z?

Or am I missing something?

Thank you!

Hello @mark.aronin,

In our quickstart module, we have two examples of tracking keyboard events via focus and blur as well as another example of using operator modifiers keys. To get started:

  1. Launch the server by double-clicking start_server.bat found in the directory HOOPS_Communicator_2024.X.x\quick_start

  2. Click the following URLs to view the aforementioned examples:

Hope this helps,
Tino

There are modifier keys for mouse input, but not for keyboard inputs. Also, those examples don’t really provide the solution that I am looking for.

I want to be able to tell if I press Ctrl + Z in my operator through my onKeyDown events. onMouseDown has getModifiers() function which is exactly what I need but for onKeyDown…

@mark.aronin,

For the the focus input example, you can see the code used in focus_input.js:

As you can see in the screenshot, there is an example of using onKeyDown(event) to get the key pressed. Will this not work in your operator code?

Yeah, I can get the key code that was pressed, but I don’t know if it was pressed with a modifier key or not. That’s my issue.

meanwhile, I am able to get modifier keys onMouseDown

It’s possible to listen for multiple keys pressed – including modifier keys. Below is an example of listening for Control + Alt + Z:

//need variables
constructor() {
  this.isControlPressed = false;
  this.isShiftPressed = false;
  this.isZPressed = false;
}

onKeyDown(event) {
    //this._logMessage(`Key Down: ${event.getKeyCode()}`);
	const keyCode = event.getKeyCode();

  if (keyCode === 17) { // control key
    this.isControlPressed = true;
  } else if (keyCode === 16) { // Shift key
    this.isShiftPressed = true;
  } else if (keyCode === 90) { // 'Z' key
    this.isZPressed = true;
  }

  if (this.isControlPressed && this.isShiftPressed && this.isZPressed) {
    this._logMessage('Control + Shift + Z pressed');
  } else {
    this._logMessage(`Key Down: ${keyCode}`);
  }
  }
  //Reset the states back to false
  onKeyUp(event) {
    this._logMessage(`Key Up: ${event.getKeyCode()}`);
	const keyCode = event.getKeyCode();

  if (keyCode === 17) {
    this.isControlPressed = false;
  } else if (keyCode === 16) {
    this.isShiftPressed = false;
  } else if (keyCode === 90) {
    this.isZPressed = false;
  }
  }

To keep it easier, member variables are added in the contructor. Also onKeyUp() needs to reset the added variables back to false.

1 Like

We tried something like this, but this doesn’t work as soon as the focus on the hoops viewer is gone. Let’s say you do an “alt+tab”. onKeyDown will record alt as being pressed down, but now you are no longer in the viewer, and you let go of the alt key. When you come back to the viewer, the alt key is still flagged as true, even though you are not holding it down.

This method is not reliable. Our next solution was to make a separate service which could track the keys being pressed down.

I was just hoping that there was already a solution from Hoops on the matter :confused: