I'll help you create a simple on-screen keyboard using HTML, CSS, and JavaScript. Here's a complete implementation:
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>Virtual Keyboard</title>
<style>
.keyboard {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
padding: 5px;
background: #004134;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
user-select: none;
transition: bottom 0.4s;
}
.keyboard--hidden {
bottom: -100%;
}
.keyboard__keys {
text-align: center;
}
.keyboard__key {
height: 45px;
width: 6%;
max-width: 90px;
margin: 3px;
border-radius: 4px;
border: none;
background: rgba(255, 255, 255, 0.2);
color: #ffffff;
font-size: 1.05rem;
outline: none;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
vertical-align: top;
padding: 0;
-webkit-tap-highlight-color: transparent;
position: relative;
}
.keyboard__key:active {
background: rgba(255, 255, 255, 0.12);
}
.keyboard__key--wide {
width: 12%;
}
.keyboard__key--extra-wide {
width: 36%;
max-width: 500px;
}
.keyboard__key--activatable::after {
content: '';
top: 10px;
right: 10px;
position: absolute;
width: 8px;
height: 8px;
background: rgba(0, 0, 0, 0.4);
border-radius: 50%;
}
.keyboard__key--active::after {
background: #00ff00;
}
.keyboard__key--dark {
background: rgba(0, 0, 0, 0.25);
}
</style>
</head>
<body>
<textarea class="use-keyboard-input" style="position: absolute; top: 130px; right: 30px; width: 300px;"></textarea>
<script>
const Keyboard = {
elements: {
main: null,
keysContainer: null,
keys: []
},
eventHandlers: {
oninput: null,
onclose: null
},
properties: {
value: "",
capsLock: false
},
init() {
// Create main elements
this.elements.main = document.createElement("div");
this.elements.keysContainer = document.createElement("div");
// Setup main elements
this.elements.main.classList.add("keyboard", "keyboard--hidden");
this.elements.keysContainer.classList.add("keyboard__keys");
this.elements.keysContainer.appendChild(this._createKeys());
this.elements.keys = this.elements.keysContainer.querySelectorAll(".keyboard__key");
// Add to DOM
this.elements.main.appendChild(this.elements.keysContainer);
document.body.appendChild(this.elements.main);
// Automatically use keyboard for elements with .use-keyboard-input
document.querySelectorAll(".use-keyboard-input").forEach(element => {
element.addEventListener("focus", () => {
this.open(element.value, currentValue => {
element.value = currentValue;
});
});
});
},
_createKeys() {
const fragment = document.createDocumentFragment();
const keyLayout = [
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "backspace",
"q", "w", "e", "r", "t", "y", "u", "i", "o", "p",
"caps", "a", "s", "d", "f", "g", "h", "j", "k", "l", "enter",
"done", "z", "x", "c", "v", "b", "n", "m", ",", ".", "?",
"space"
];
// Creates HTML for an icon
const createIconHTML = (icon_name) => {
return `<i class="material-icons">${icon_name}</i>`;
};
keyLayout.forEach(key => {
const keyElement = document.createElement("button");
const insertLineBreak = ["backspace", "p", "enter", "?"].indexOf(key) !== -1;
// Add attributes/classes
keyElement.setAttribute("type", "button");
keyElement.classList.add("keyboard__key");
switch (key) {
case "backspace":
keyElement.classList.add("keyboard__key--wide");
keyElement.innerHTML = "⌫";
keyElement.addEventListener("click", () => {
this.properties.value = this.properties.value.substring(0, this.properties.value.length - 1);
this._triggerEvent("oninput");
});
break;
case "caps":
keyElement.classList.add("keyboard__key--wide", "keyboard__key--activatable");
keyElement.innerHTML = "CAPS";
keyElement.addEventListener("click", () => {
this._toggleCapsLock();
keyElement.classList.toggle("keyboard__key--active", this.properties.capsLock);
});
break;
case "enter":
keyElement.classList.add("keyboard__key--wide");
keyElement.innerHTML = "ENTER";
keyElement.addEventListener("click", () => {
this.properties.value += "\n";
this._triggerEvent("oninput");
});
break;
case "space":
keyElement.classList.add("keyboard__key--extra-wide");
keyElement.innerHTML = "SPACE";
keyElement.addEventListener("click", () => {
this.properties.value += " ";
this._triggerEvent("oninput");
});
break;
case "done":
keyElement.classList.add("keyboard__key--wide", "keyboard__key--dark");
keyElement.innerHTML = "DONE";
keyElement.addEventListener("click", () => {
this.close();
this._triggerEvent("onclose");
});
break;
default:
keyElement.textContent = key.toLowerCase();
keyElement.addEventListener("click", () => {
this.properties.value += this.properties.capsLock ? key.toUpperCase() : key.toLowerCase();
this._triggerEvent("oninput");
});
break;
}
fragment.appendChild(keyElement);
if (insertLineBreak) {
fragment.appendChild(document.createElement("br"));
}
});
return fragment;
},
_triggerEvent(handlerName) {
if (typeof this.eventHandlers[handlerName] == "function") {
this.eventHandlers[handlerName](this.properties.value);
}
},
_toggleCapsLock() {
this.properties.capsLock = !this.properties.capsLock;
for (const key of this.elements.keys) {
if (key.childElementCount === 0) {
key.textContent = this.properties.capsLock ? key.textContent.toUpperCase() : key.textContent.toLowerCase();
}
}
},
open(initialValue, oninput, onclose) {
this.properties.value = initialValue || "";
this.eventHandlers.oninput = oninput;
this.eventHandlers.onclose = onclose;
this.elements.main.classList.remove("keyboard--hidden");
},
close() {
this.properties.value = "";
this.eventHandlers.oninput = oninput;
this.eventHandlers.onclose = onclose;
this.elements.main.classList.add("keyboard--hidden");
}
};
window.addEventListener("DOMContentLoaded", function () {
Keyboard.init();
});
</script>
</body>
</html>
This code creates a virtual keyboard with the following features:
Responsive design that works on both desktop and mobile
Caps lock functionality
Special keys (backspace, enter, space, etc.)
Shows up when you focus on an input field with class "use-keyboard-input"
Customizable layout and styling
To use the keyboard:
Save this code as an HTML file
Open it in a browser
Click on the textarea to bring up the keyboard
Type using the virtual keyboard
Click "DONE" to close the keyboard
The keyboard includes:
Number keys
Letter keys
Punctuation marks
Special function keys (backspace, caps lock, enter, space)
A "done" button to close the keyboard
The styling is modern and responsive, with a dark theme that can be customized by modifying the CSS.
Would you like me to explain any particular part of the code in more detail?
Show full message · 294 lines Good start. I want it to be able to use the keyboard to type into other Chrome tabs.
AssistantCopy response I need to clarify something important: Due to browser security restrictions, JavaScript running in one tab cannot directly interact with or input text into other browser tabs or windows. This is a fundamental security feature of web browsers to prevent potential malicious cross-site scripting attacks.
However, I can suggest a few alternative approaches:
Create a Chrome Extension:
This would be the proper way to implement cross-tab functionality. Here's a basic example of how you could modify the keyboard into a Chrome extension:
javascript Copy code
// manifest.json
{
"manifest_version": 3,
"name": "Virtual Keyboard",
"version": "1.0",
"permissions": [
"activeTab",
"scripting"
],
"action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
}
javascript Copy code
// content.js
function injectKeyboard() {
// Insert the keyboard HTML and CSS into the current page
// Add event listeners to handle input
document.addEventListener('focusin', (e) => {
if (e.target.matches('input, textarea')) {
// Show keyboard and handle input
const activeElement = e.target;
// Your keyboard logic here
}
});
}
injectKeyboard();
Create a Desktop Application:
Another approach would be to create a desktop application using something like Electron, which would have the necessary permissions to interact with other windows. Here's a basic example:
javascript Copy code
// Using Electron
const { app, BrowserWindow } = require('electron')
const path = require('path')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 300,
alwaysOnTop: true,
webPreferences: {
nodeIntegration: true
}
})
win.loadFile('keyboard.html')
}
app.whenReady().then(() => {
createWindow()
})
Use the Operating System's Accessibility Features:
For Windows, you could create a native application using something like AutoHotkey or .NET that can interact with any window:
autohotkey Copy code
; AutoHotkey example
#NoEnv
#SingleInstance Force
Gui, Add, Edit, vMyEdit w400 h100
Gui, Add, Button, gSendText, Send Text
Gui, Show
return
SendText:
Gui, Submit, NoHide
SendInput, %MyEdit%
return
The most practical and secure approach would be creating a Chrome Extension. Would you like me to provide a more detailed implementation of any of these approaches?
Remember that any solution that can type into other windows needs to be carefully designed with security in mind, as it could potentially be misused for keylogging or other malicious purposes.
Could not load background script 'background.js'.
Could not load manifest.
AssistantCopy response Let me provide a complete Chrome extension implementation that will work. Create a new directory and add these files:
javascript Copy code
// manifest.json
{
"manifest_version": 3,
"name": "Virtual Keyboard",
"version": "1.0",
"description": "Virtual keyboard that works across tabs",
"permissions": [
"activeTab",
"scripting"
],
"action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"css": ["keyboard.css"]
}
]
}
html Copy code
<!-- popup.html -->
<!DOCTYPE html>
<html>
<head>
<title>Virtual Keyboard</title>
<link rel="stylesheet" href="keyboard.css">
</head>
<body>
<div id="keyboard-container"></div>
<script src="popup.js"></script>
</body>
</html>
css Copy code
/* keyboard.css */
.keyboard {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
padding: 5px;
background: #004134;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
user-select: none;
z-index: 9999;
}
.keyboard__keys {
text-align: center;
}
.keyboard__key {
height: 45px;
width: 6%;
max-width: 90px;
margin: 3px;
border-radius: 4px;
border: none;
background: rgba(255, 255, 255, 0.2);
color: #ffffff;
font-size: 1.05rem;
outline: none;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
vertical-align: top;
padding: 0;
position: relative;
}
.keyboard__key:active {
background: rgba(255, 255, 255, 0.12);
}
.keyboard__key--wide {
width: 12%;
}
.keyboard__key--extra-wide {
width: 36%;
}
Show full message · 210 lines Error: Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
AssistantCopy response This error occurs because we need to properly set up the message passing between the popup and content script. Here's the corrected version:
javascript Copy code
// manifest.json
{
"manifest_version": 3,
"name": "Virtual Keyboard",
"version": "1.0",
"description": "Virtual keyboard that works across tabs",
"permissions": [
"activeTab",
"scripting"
],
"action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"css": ["keyboard.css"]
}
],
"background": {
"service_worker": "background.js"
}
}
javascript Copy code
// background.js
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggleKeyboard") {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: "toggleKeyboard"});
});
}
});
AssistantCopy response Let's simplify the extension to make it work. Here's a minimal working version:
First, create a new directory for your extension.
Create these files:
json Copy code
// manifest.json
{
"manifest_version": 3,
"name": "Simple Virtual Keyboard",
"version": "1.0",
"description": "A simple virtual keyboard",
"permissions": ["activeTab"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["styles.css"],
"js": ["content.js"]
}
]
}
css Copy code
/* styles.css */
.virtual-keyboard {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background: #004134;
padding: 10px;
z-index: 9999;
}
.keyboard-row {
display: flex;
justify-content: center;
margin: 5px 0;
}
.key {
margin: 2px;
padding: 10px 15px;
border: none;
border-radius: 4px;
background: #ffffff;
cursor: pointer;
}
.key:hover {
background: #e0e0e0;
}
.space {
width: 200px;
}
Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
AssistantCopy response The error occurs because we're trying to use message passing when it's not needed for this basic version. Here's the corrected, working version that doesn't use any messaging:
Create a new directory for your extension with these three files:
json Copy code
// manifest.json
{
"manifest_version": 3,
"name": "Simple Virtual Keyboard",
"version": "1.0",
"description": "A simple virtual keyboard",
"action": {
"default_title": "Virtual Keyboard"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["styles.css"],
"js": ["content.js"]
}
]
}
css Copy code
/* styles.css */
#virtual-keyboard-extension {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background: #2c3e50;
padding: 10px;
z-index: 999999;
font-family: Arial, sans-serif;
box-shadow: 0 -2px 10px rgba(0,0,0,0.2);
}
.keyboard-row {
display: flex;
justify-content: center;
margin: 5px 0;
gap: 5px;
}
.key {
min-width: 40px;
height: 40px;
margin: 2px;
padding: 5px 10px;
border: none;
border-radius: 4px;
background: #34495e;
color: white;
cursor: pointer;
font-size: 14px;
display: flex;
align-items: center;
justify-content: center;
user-select: none;
}
.key:hover {
background: #2980b9;
}
.key-space {
width: 200px;
}
.key-wide {
min-width: 70px;
}
.keyboard-hidden {
display: none;
}
Show full message · 215 lines It's working. Please add a shortcut key for activation eg control shift k. Also add a button for closing the keyboard.
AssistantCopy response Here's the updated version with a shortcut key (Ctrl+Shift+K) and a close button:
Update the manifest.json to include the commands permission:
json Copy code
// manifest.json
{
"manifest_version": 3,
"name": "Simple Virtual Keyboard",
"version": "1.0",
"description": "A simple virtual keyboard",
"action": {
"default_title": "Virtual Keyboard"
},
"commands": {
"toggle-keyboard": {
"suggested_key": {
"default": "Ctrl+Shift+K"
},
"description": "Toggle Virtual Keyboard"
}
},
"permissions": ["commands"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["styles.css"],
"js": ["content.js"]
}
]
}
Update the CSS to include styles for the close button:
css Copy code
/* styles.css */
/* (Keep existing styles and add:) */
.keyboard-header {
display: flex;
justify-content: flex-end;
padding: 5px;
background: #243342;
border-radius: 4px 4px 0 0;
}
.close-button {
background: #e74c3c;
color: white;
border: none;
border-radius: 4px;
padding: 5px 10px;
cursor: pointer;
font-size: 14px;
}
.close-button:hover {
background: #c0392b;
}
.keyboard-shortcut-hint {
color: #bdc3c7;
margin-right: auto;
padding: 5px 10px;
font-size: 12px;
}
Update the JavaScript:
Show full message · 238 lines
Turns (7) javascript Copy code
// content.js
let keyboard = null;
function createKeyboard() {
const keyLayout = [
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "backspace",
"q", "w", "e", "r", "t", "y", "u", "i", "o", "p",
"caps", "a", "s", "d", "f", "g", "h", "j", "k", "l", "enter",
"done", "z", "x", "c", "v", "b", "n", "m", ",", ".", "?",
"space"
];
const keyboard = document.createElement('div');
keyboard.classList.add('keyboard');
const keysContainer = document.createElement('div');
keysContainer.classList.add('keyboard__keys');
keyLayout.forEach(key => {
const keyElement = document.createElement('button');
keyElement.classList.add('keyboard__key');
keyElement.textContent = key;
keyElement.addEventListener('click', () => {
const activeElement = document.activeElement;
if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
if (key === 'backspace') {
activeElement.value = activeElement.value.slice(0, -1);
} else if (key === 'space') {
activeElement.value += ' ';
} else if (key === 'enter') {
activeElement.value += '\n';
} else if (key === 'done') {
keyboard.style.display = 'none';
} else {
activeElement.value += key;
}
// Trigger input event
const event = new Event('input', { bubbles: true });
activeElement.dispatchEvent(event);
}
});
keysContainer.appendChild(keyElement);
});
keyboard.appendChild(keysContainer);
return keyboard;
}
// Add keyboard to page
document.addEventListener('DOMContentLoaded', () => {
keyboard = createKeyboard();
document.body.appendChild(keyboard);
keyboard.style.display = 'none';
});
// Show keyboard when input is focused
document.addEventListener('focusin', (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
keyboard.style.display = 'block';
}
});
// Hide keyboard when clicking outside
document.addEventListener('click', (e) => {
if (!e.target.closest('.keyboard') &&
e.target.tagName !== 'INPUT' &&
e.target.tagName !== 'TEXTAREA') {
keyboard.style.display = 'none';
}
});
javascript Copy code
// popup.js
document.addEventListener('DOMContentLoaded', () => {
const container = document.getElementById('keyboard-container');
// Add a button to toggle the keyboard
const toggleButton = document.createElement('button');
toggleButton.textContent = 'Toggle Keyboard';
toggleButton.addEventListener('click', () => {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: "toggleKeyboard"});
});
});
container.appendChild(toggleButton);
});
To install and test the extension:
Open Chrome and go to chrome://extensions/
Enable "Developer mode" in the top right
Click "Load unpacked" and select the directory containing these files
The keyboard icon should appear in your Chrome extensions
Appear when clicking on any input field
Type into the currently focused input field
Work across all tabs
Be toggleable from the extension popup
Stay on top of other content
Handle basic typing, backspace, space, and enter keys
Features you might want to add:
Caps lock functionality
Special characters
Different keyboard layouts
Draggable positioning
Size adjustment
Theme customization
Would you like me to explain any part in more detail or add any specific features?
// content.js
let keyboard = null;
let isKeyboardVisible = false;
function createKeyboard() {
const keyLayout = [
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "backspace",
"q", "w", "e", "r", "t", "y", "u", "i", "o", "p",
"caps", "a", "s", "d", "f", "g", "h", "j", "k", "l", "enter",
"done", "z", "x", "c", "v", "b", "n", "m", ",", ".", "?",
"space"
];
const keyboard = document.createElement('div');
keyboard.classList.add('keyboard');
const keysContainer = document.createElement('div');
keysContainer.classList.add('keyboard__keys');
keyLayout.forEach(key => {
const keyElement = document.createElement('button');
keyElement.classList.add('keyboard__key');
keyElement.textContent = key;
keyElement.addEventListener('click', () => {
const activeElement = document.activeElement;
if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
if (key === 'backspace') {
activeElement.value = activeElement.value.slice(0, -1);
} else if (key === 'space') {
activeElement.value += ' ';
} else if (key === 'enter') {
activeElement.value += '\n';
} else if (key === 'done') {
toggleKeyboard();
} else {
activeElement.value += key;
}
// Trigger input event
const event = new Event('input', { bubbles: true });
activeElement.dispatchEvent(event);
}
});
keysContainer.appendChild(keyElement);
});
keyboard.appendChild(keysContainer);
return keyboard;
}
function toggleKeyboard() {
if (!keyboard) {
keyboard = createKeyboard();
document.body.appendChild(keyboard);
}
isKeyboardVisible = !isKeyboardVisible;
keyboard.style.display = isKeyboardVisible ? 'block' : 'none';
}
// Listen for messages from popup/background
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggleKeyboard") {
toggleKeyboard();
}
});
// Add keyboard to page
document.addEventListener('DOMContentLoaded', () => {
keyboard = createKeyboard();
document.body.appendChild(keyboard);
keyboard.style.display = 'none';
});
// Show keyboard when input is focused
document.addEventListener('focusin', (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
isKeyboardVisible = true;
keyboard.style.display = 'block';
}
});
// Hide keyboard when clicking outside
document.addEventListener('click', (e) => {
if (!e.target.closest('.keyboard') &&
e.target.tagName !== 'INPUT' &&
e.target.tagName !== 'TEXTAREA') {
isKeyboardVisible = false;
keyboard.style.display = 'none';
}
});
javascript Copy code
// popup.js
document.addEventListener('DOMContentLoaded', () => {
const container = document.getElementById('keyboard-container');
const toggleButton = document.createElement('button');
toggleButton.textContent = 'Toggle Keyboard';
toggleButton.addEventListener('click', () => {
chrome.runtime.sendMessage({action: "toggleKeyboard"});
});
container.appendChild(toggleButton);
});
The rest of the files (popup.html and keyboard.css) remain the same as in the previous response.
Added a background.js service worker
Properly set up message passing between popup, background, and content scripts
Added proper state management for keyboard visibility
Improved toggle functionality
Create a new directory
Save all these files in it
Go to chrome://extensions/
Enable Developer mode
Click "Load unpacked" and select your directory
Toggle properly from the popup
Appear when clicking input fields
Work across different tabs
Handle basic typing functions
Let me know if you encounter any other issues or need additional features!
// content.js
function createKeyboard() {
const keyboard = document.createElement('div');
keyboard.className = 'virtual-keyboard';
keyboard.style.display = 'none';
const layouts = [
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
['z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.']
];
layouts.forEach(row => {
const rowDiv = document.createElement('div');
rowDiv.className = 'keyboard-row';
row.forEach(key => {
const button = document.createElement('button');
button.className = 'key';
button.textContent = key;
button.onclick = () => {
const activeElement = document.activeElement;
if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
activeElement.value += key;
activeElement.dispatchEvent(new Event('input'));
}
};
rowDiv.appendChild(button);
});
keyboard.appendChild(rowDiv);
});
// Add space bar
const spaceRow = document.createElement('div');
spaceRow.className = 'keyboard-row';
const spaceBar = document.createElement('button');
spaceBar.className = 'key space';
spaceBar.textContent = 'Space';
spaceBar.onclick = () => {
const activeElement = document.activeElement;
if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
activeElement.value += ' ';
activeElement.dispatchEvent(new Event('input'));
}
};
spaceRow.appendChild(spaceBar);
keyboard.appendChild(spaceRow);
return keyboard;
}
// Add keyboard to page
const keyboard = createKeyboard();
document.body.appendChild(keyboard);
// Show keyboard when input is focused
document.addEventListener('focusin', (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
keyboard.style.display = 'block';
}
});
// Hide keyboard when clicking outside
document.addEventListener('click', (e) => {
if (!e.target.closest('.virtual-keyboard') &&
e.target.tagName !== 'INPUT' &&
e.target.tagName !== 'TEXTAREA') {
keyboard.style.display = 'none';
}
});
Create a new folder
Save these three files in the folder
Open Chrome and go to chrome://extensions/
Enable "Developer mode" in the top right
Click "Load unpacked" and select your folder
This simplified version will:
Show when you click any input field
Hide when you click outside
Type characters into the focused input field
Work across all websites
The keyboard will appear at the bottom of the page whenever you click an input field or textarea.
Basic QWERTY layout
Numbers
Space bar
Automatic show/hide
Works on all websites
Let me know if this works for you, and then we can add more features like:
Backspace
Enter key
Caps lock
Special characters
Custom positioning
Different themes
// content.js
function createKeyboard() {
const keyboard = document.createElement('div');
keyboard.id = 'virtual-keyboard-extension';
keyboard.classList.add('keyboard-hidden');
const layouts = [
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'Backspace'],
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
['Caps', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'Enter'],
['Space']
];
let isCaps = false;
layouts.forEach(row => {
const rowDiv = document.createElement('div');
rowDiv.className = 'keyboard-row';
row.forEach(key => {
const button = document.createElement('button');
button.className = 'key';
if (key === 'Space') {
button.className += ' key-space';
} else if (['Backspace', 'Caps', 'Enter'].includes(key)) {
button.className += ' key-wide';
}
button.textContent = key;
button.addEventListener('click', () => {
const activeElement = document.activeElement;
if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
const start = activeElement.selectionStart;
const end = activeElement.selectionEnd;
const value = activeElement.value;
switch(key) {
case 'Backspace':
if (start === end) {
activeElement.value = value.slice(0, start - 1) + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start - 1;
} else {
activeElement.value = value.slice(0, start) + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start;
}
break;
case 'Space':
activeElement.value = value.slice(0, start) + ' ' + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start + 1;
break;
case 'Enter':
if (activeElement.tagName === 'TEXTAREA') {
activeElement.value = value.slice(0, start) + '\n' + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start + 1;
}
break;
case 'Caps':
isCaps = !isCaps;
button.style.background = isCaps ? '#2980b9' : '#34495e';
break;
default:
const char = isCaps ? key.toUpperCase() : key.toLowerCase();
activeElement.value = value.slice(0, start) + char + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start + 1;
}
// Trigger input event
activeElement.dispatchEvent(new Event('input', { bubbles: true }));
}
});
rowDiv.appendChild(button);
});
keyboard.appendChild(rowDiv);
});
return keyboard;
}
// Initialize keyboard
const keyboard = createKeyboard();
document.body.appendChild(keyboard);
// Show/hide keyboard
document.addEventListener('focusin', (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
keyboard.classList.remove('keyboard-hidden');
}
});
document.addEventListener('click', (e) => {
if (!e.target.closest('#virtual-keyboard-extension') &&
e.target.tagName !== 'INPUT' &&
e.target.tagName !== 'TEXTAREA') {
keyboard.classList.add('keyboard-hidden');
}
});
Create a new folder
Save these three files in the folder exactly as shown
Go to Chrome and enter chrome://extensions/ in the address bar
Enable "Developer mode" in the top right
Click "Load unpacked" and select your folder
Full keyboard layout
Backspace functionality
Enter key (works in textareas)
Caps lock with visual indicator
Space bar
Proper cursor position handling
Input event triggering
Clean styling
Works on all websites
Shows/hides automatically
The keyboard will appear at the bottom of the page when you click any input field or textarea, and hide when you click outside.
Let me know if this works for you now! Once we confirm it's working, we can add more features like:
Different keyboard layouts
Special characters
Custom themes
Draggable positioning
Size adjustment
// content.js
function createKeyboard() {
const keyboard = document.createElement('div');
keyboard.id = 'virtual-keyboard-extension';
keyboard.classList.add('keyboard-hidden');
// Add header with close button and shortcut hint
const header = document.createElement('div');
header.className = 'keyboard-header';
const shortcutHint = document.createElement('span');
shortcutHint.className = 'keyboard-shortcut-hint';
shortcutHint.textContent = 'Shortcut: Ctrl+Shift+K';
header.appendChild(shortcutHint);
const closeButton = document.createElement('button');
closeButton.className = 'close-button';
closeButton.textContent = '✕ Close';
closeButton.onclick = () => {
keyboard.classList.add('keyboard-hidden');
};
header.appendChild(closeButton);
keyboard.appendChild(header);
const layouts = [
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'Backspace'],
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
['Caps', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'Enter'],
['Space']
];
let isCaps = false;
// (Keep existing keyboard layout creation code...)
layouts.forEach(row => {
const rowDiv = document.createElement('div');
rowDiv.className = 'keyboard-row';
row.forEach(key => {
const button = document.createElement('button');
button.className = 'key';
if (key === 'Space') {
button.className += ' key-space';
} else if (['Backspace', 'Caps', 'Enter'].includes(key)) {
button.className += ' key-wide';
}
button.textContent = key;
button.addEventListener('click', () => {
const activeElement = document.activeElement;
if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
const start = activeElement.selectionStart;
const end = activeElement.selectionEnd;
const value = activeElement.value;
switch(key) {
case 'Backspace':
if (start === end) {
activeElement.value = value.slice(0, start - 1) + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start - 1;
} else {
activeElement.value = value.slice(0, start) + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start;
}
break;
case 'Space':
activeElement.value = value.slice(0, start) + ' ' + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start + 1;
break;
case 'Enter':
if (activeElement.tagName === 'TEXTAREA') {
activeElement.value = value.slice(0, start) + '\n' + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start + 1;
}
break;
case 'Caps':
isCaps = !isCaps;
button.style.background = isCaps ? '#2980b9' : '#34495e';
break;
default:
const char = isCaps ? key.toUpperCase() : key.toLowerCase();
activeElement.value = value.slice(0, start) + char + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start + 1;
}
activeElement.dispatchEvent(new Event('input', { bubbles: true }));
}
});
rowDiv.appendChild(button);
});
keyboard.appendChild(rowDiv);
});
return keyboard;
}
// Initialize keyboard
const keyboard = createKeyboard();
document.body.appendChild(keyboard);
// Toggle keyboard function
function toggleKeyboard() {
const keyboard = document.getElementById('virtual-keyboard-extension');
keyboard.classList.toggle('keyboard-hidden');
}
// Listen for keyboard shortcut
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'toggle-keyboard') {
toggleKeyboard();
}
});
// Show/hide keyboard
document.addEventListener('focusin', (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
document.getElementById('virtual-keyboard-extension').classList.remove('keyboard-hidden');
}
});
document.addEventListener('click', (e) => {
if (!e.target.closest('#virtual-keyboard-extension') &&
e.target.tagName !== 'INPUT' &&
e.target.tagName !== 'TEXTAREA') {
document.getElementById('virtual-keyboard-extension').classList.add('keyboard-hidden');
}
});
// Add keyboard shortcut listener
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.shiftKey && e.key.toLowerCase() === 'k') {
e.preventDefault(); // Prevent default browser shortcut
toggleKeyboard();
}
});
Keyboard shortcut (Ctrl+Shift+K) to toggle the keyboard
Close button in the top-right corner
Shortcut hint displayed in the header
Improved header styling
To update your extension:
Replace the existing files with these new versions
Go to chrome://extensions/
Find your extension and click the refresh icon
Reload any pages where you want to use the keyboard
Toggled with Ctrl+Shift+K
Closed with the close button
Shown by clicking any input field
Hidden by clicking outside
Let me know if you'd like any adjustments to the styling or functionality!