-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxpkeysender.lua
More file actions
108 lines (92 loc) · 3.03 KB
/
Copy pathxpkeysender.lua
File metadata and controls
108 lines (92 loc) · 3.03 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
local socket = require("socket")
local listener_ip = "127.0.0.1"
local listener_port = 27900
-- Key that will be sent to virtual machine
local key1_name = "n"
local key2_name = "m"
local key3_name = "k"
-- Global TCP client socket variable
local client = nil
-- Function to create and connect the socket once
local function connect()
if client ~= nil then
return true -- already connected
end
client = socket.tcp()
client:settimeout(0) -- non-blocking
local success, err = client:connect(listener_ip, listener_port)
if not success and err ~= "timeout" then
logMsg("Failed to connect to VM: " .. tostring(err))
client = nil
return false
end
logMsg("Connected to VM at " .. listener_ip .. ":" .. listener_port)
return true
end
-- Function to send command over the persistent socket
local function send_command(command)
if client == nil then
local ok = connect()
if not ok then
logMsg("Cannot send command, not connected")
return
end
end
local send_ok, send_err = client:send(command .. "\n")
if not send_ok then
logMsg("Failed to send data: " .. tostring(send_err))
client:close()
client = nil
return
end
-- Try to read response (non-blocking)
local response, recv_err = client:receive("*l")
if response then
logMsg("VM Response: " .. response)
elseif recv_err ~= "timeout" then
logMsg("Receive error: " .. tostring(recv_err))
client:close()
client = nil
end
end
-- Key press/release functions
function send_key_press(key_name)
send_command("hold_" .. key_name)
end
function send_key_release(key_name)
send_command("release_" .. key_name)
end
-- Create command to trigger on key press/release in X-Plane
create_command(
"FlyWithLua/xpkeysender/send_key1",
"Send key press 1 to VM",
"send_key_press('" .. key1_name .. "')", -- on key press
"", -- do nothing while holding the key in X-Plane
"send_key_release('" .. key1_name .. "')" -- on key release
)
create_command(
"FlyWithLua/xpkeysender/send_key2",
"Send key press 2 to VM",
"send_key_press('" .. key2_name .. "')", -- on key press
"", -- do nothing while holding the key in X-Plane
"send_key_release('" .. key2_name .. "')" -- on key release
)
create_command(
"FlyWithLua/xpkeysender/send_key3",
"Send key press 3 to VM",
"send_key_press('" .. key3_name .. "')", -- on key press
"", -- do nothing while holding the key in X-Plane
"send_key_release('" .. key3_name .. "')" -- on key release
)
-- Try reconnecting every 5 seconds if disconnected
local last_connect_attempt = 0
do_every_frame("try_reconnect()")
function try_reconnect()
if client == nil then
local t = os.clock()
if t - last_connect_attempt > 5 then
connect()
last_connect_attempt = t
end
end
end