Source: app/components/VirtualKeyboard.vue

<!--
  Copyright 2025 The Ray Optics Simulation authors and contributors

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<template>
  <div class="virtual-keyboard" v-show="lastDeviceIsTouch">
    <button 
      class="virtual-key" 
      :class="{ active: ctrlActive }"
      type="button"
      @click="ctrlActive = !ctrlActive"
    >Ctrl</button>
    <button 
      class="virtual-key" 
      :class="{ active: shiftActive }"
      type="button"
      @click="shiftActive = !shiftActive"
    >Shift</button>
  </div>
</template>

<script>
/**
 * @module VirtualKeyboard
 * @description The Vue component for virtual Ctrl and Shift keys for touchscreen use.
 */
import { ref, watch, onMounted } from 'vue'
import { statusEmitter, STATUS_EVENT_NAMES } from '../composables/useStatus'
import { app } from '../services/app'

export default {
  name: 'VirtualKeyboard',
  setup() {
    const ctrlActive = ref(false)
    const shiftActive = ref(false)
    
    // Check if mobile layout is shown (Bootstrap lg breakpoint is 992px)
    const isMobileLayout = () => window.matchMedia('(max-width: 991.98px)').matches
    const lastDeviceIsTouch = ref(isMobileLayout())

    // Update editor's lastDeviceIsTouch if available
    onMounted(() => {
      if (isMobileLayout() && app.editor) {
        app.editor._lastDeviceIsTouch = true
      }
    })

    statusEmitter.on(STATUS_EVENT_NAMES.DEVICE_CHANGE, (e) => {
      lastDeviceIsTouch.value = e.lastDeviceIsTouch
    })

    statusEmitter.on(STATUS_EVENT_NAMES.RESET_VIRTUAL_KEYS, () => {
      ctrlActive.value = false
      shiftActive.value = false
    })

    // Sync virtual key states with the editor
    watch(ctrlActive, (newValue) => {
      if (app.editor) {
        app.editor.virtualCtrlKey = newValue
      }
    })

    watch(shiftActive, (newValue) => {
      if (app.editor) {
        app.editor.virtualShiftKey = newValue
      }
    })

    return {
      ctrlActive,
      shiftActive,
      lastDeviceIsTouch
    }
  }
}
</script>

<style scoped>
.virtual-keyboard {
  display: flex;
  gap: 7px;
  padding: 4px;
  pointer-events: auto;
}

.virtual-key {
  color: gray;
  background-color: transparent;
  border: 1px solid gray;
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 12px;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
  line-height: 1;
}

.virtual-key:hover {
  background-color: rgba(128, 128, 128, 0.3);
}

.virtual-key:focus {
  outline: none;
  box-shadow: none;
}

.virtual-key:active {
  background-color: rgba(128, 128, 128, 0.3);
}

.virtual-key.active {
  color: white;
  background-color: gray;
}

.virtual-key.active:hover {
  background-color: gray;
}
</style>