-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphone-keyboard-server.js
More file actions
120 lines (102 loc) · 3.29 KB
/
phone-keyboard-server.js
File metadata and controls
120 lines (102 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require('express');
const { WebSocketServer } = require('ws');
const http = require('http');
const path = require('path');
const app = express();
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
const PORT = process.env.PORT || 3000;
// Serve static files
app.use(express.static('public'));
// Store connected clients
const clients = new Set();
// WebSocket connection handler
wss.on('connection', (ws) => {
console.log('Client connected');
clients.add(ws);
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
if (data.type === 'keypress') {
console.log('Received keypress:', data);
handleKeypress(data);
}
} catch (error) {
console.error('Error processing message:', error);
}
});
ws.on('close', () => {
console.log('Client disconnected');
clients.delete(ws);
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
});
// Handle keyboard input and simulate keypresses
function handleKeypress(data) {
const { key, modifiers, isSpecialKey } = data;
try {
// Check if robotjs is available
const robot = require('robotjs');
// Handle special keys that need keyTap
const keyMap = {
'Backspace': 'backspace',
'Enter': 'enter',
'Tab': 'tab',
'Escape': 'escape',
'ArrowUp': 'up',
'ArrowDown': 'down',
'ArrowLeft': 'left',
'ArrowRight': 'right',
'Delete': 'delete',
'Home': 'home',
'End': 'end',
'PageUp': 'pageup',
'PageDown': 'pagedown'
};
// Apply modifiers if present
const modArray = [];
if (modifiers?.ctrl) modArray.push('control');
if (modifiers?.alt) modArray.push('alt');
if (modifiers?.shift) modArray.push('shift');
if (modifiers?.meta) modArray.push('command');
// If it's a special key or has modifiers, use keyTap
if (isSpecialKey || modArray.length > 0 ) {
const robotKey = keyMap[key] || key.toLowerCase();
robot.keyTap(robotKey, modArray);
console.log(`Simulated keypress: ${robotKey}`, modArray.length > 0 ? `with modifiers: ${modArray}` : '');
} else {
// For regular characters (including symbols), use typeString
robot.typeString(key);
console.log(`Typed character: ${key}`);
}
} catch (error) {
console.error('Error simulating keypress:', error.message);
console.log('Install robotjs with: npm install robotjs');
}
}
// Get local IP address
function getLocalIP() {
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip internal and non-IPv4 addresses
if (net.family === 'IPv4' && !net.internal) {
return net.address;
}
}
}
return 'localhost';
}
server.listen(PORT, '0.0.0.0', () => {
const localIP = getLocalIP();
console.log('\n=================================');
console.log('Phone Keyboard Server Started');
console.log('=================================');
console.log(`Local: http://localhost:${PORT}`);
console.log(`Network: http://${localIP}:${PORT}`);
console.log('=================================');
console.log('\nOpen the Network URL on your phone to use it as a keyboard\n');
});