2

I have an OpenLayers select interaction with an add condition event handler.

const addCondition = (event: MapBrowserEvent):boolean => {

  return true;
}

In this handler I need to determine if the CTRL key was pressed when the user clicked on a map feature. It does not appear the MapBrowserEvent contains the information to determine if the CTRL key was pressed. Is there a way to do this?

2
  • 1
    Use ol/events/condition.platformModifierKeyOnly
    – Mike
    Commented Feb 27, 2020 at 19:40
  • I am using platformModifierKeyOnly but the user can still click on the feature or CTRL click. I need to determine whether the CTRL key was pressed when the user clicked.
    – ChrisP
    Commented Feb 27, 2020 at 19:46

2 Answers 2

3

I'm not familiar with ES6, so this will be old school. You can check individual keys/triggers in the condition function the following way:

function addCondition(mapBrowserEvent) {
  var isClick = ol.events.condition.click(mapBrowserEvent);
  var isCtrl = ol.events.condition.platformModifierKeyOnly(mapBrowserEvent);
  if (isClick) {
    if (isCtrl) {
      ...
      }
    else {
      ...
    }
  }
}

You can then return true or false according to your liking.

0
function onSingleClick(e : MapBrowserEvent<MouseEvent>) {
  if (e.originalEvent.ctrlKey) {
    ...
  }
  else {
    ...
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.