1/* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17'use strict'; 18 19function processButton(buttonName, keyCode, dc) { 20 function onMouseDown(evt) { 21 dc.sendKeyEvent(keyCode, "keydown"); 22 } 23 24 function onMouseUp(evt) { 25 dc.sendKeyEvent(keyCode, "keyup"); 26 } 27 let button = document.getElementById(buttonName); 28 button.addEventListener('mousedown', onMouseDown); 29 button.addEventListener('mouseup', onMouseUp); 30} 31 32function processToggleButton(buttonName, keyCode, dc) { 33 let toggle = false; 34 function onMouseDown(evt) { 35 const kPrimaryButton = 1; 36 if ((evt.buttons & kPrimaryButton) == 0) { 37 return; 38 } 39 toggle = !toggle; 40 if (toggle) { 41 dc.sendKeyEvent(keyCode, "keydown"); 42 } else { 43 dc.sendKeyEvent(keyCode, "keyup"); 44 } 45 this.classList.toggle('active'); 46 } 47 48 let button = document.getElementById(buttonName); 49 button.addEventListener('mousedown', onMouseDown); 50} 51 52function enableKeyboardRewriteButton(dc) { 53 processToggleButton("shift-button", "ShiftLeft", dc); 54 processToggleButton("ctrl-button", "CtrlLeft", dc); 55 processToggleButton("alt-button", "AltLeft", dc); 56 processToggleButton("super-button", "MetaLeft", dc); 57 processButton("tab-button", "Tab", dc); 58} 59