-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from MrEliptik/global_shortcut
- Loading branch information
Showing
7 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
extends Node | ||
|
||
signal shortcut_close_game_pressed() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
extends Node | ||
|
||
class_name GlobalShortcut | ||
|
||
var ws_socket: WebSocketPeer = WebSocketPeer.new() | ||
var websocket_url = "ws://localhost:65432" | ||
|
||
func _init() -> void: | ||
set_process(false) | ||
if ws_socket.connect_to_url(websocket_url) != OK: | ||
print("Could not connect.") | ||
return | ||
|
||
print("Connected to: ", websocket_url) | ||
|
||
var timer: Timer = Timer.new() | ||
timer.one_shot = false | ||
timer.timeout.connect(process_ws) | ||
Engine.get_main_loop().root.add_child.call_deferred(timer) | ||
timer.start.call_deferred(0.5) | ||
|
||
func _ready(): | ||
set_process(false) | ||
if ws_socket.connect_to_url(websocket_url) != OK: | ||
print("Could not connect.") | ||
return | ||
|
||
print("Connected to: ", websocket_url) | ||
|
||
var timer: Timer = Timer.new() | ||
timer.one_shot = false | ||
timer.timeout.connect(process_ws) | ||
add_child(timer) | ||
timer.start(0.5) | ||
|
||
func process_ws() -> void: | ||
ws_socket.poll() | ||
|
||
if ws_socket.get_ready_state() == WebSocketPeer.STATE_OPEN: | ||
while ws_socket.get_available_packet_count(): | ||
print("WS received: ", ws_socket.get_packet().get_string_from_ascii()) | ||
# No need to check what the message is, we know we receive a signal only | ||
# when the shortcut we want is pressed | ||
SignalBus.shortcut_close_game_pressed.emit() | ||
elif ws_socket.get_ready_state() == WebSocketPeer.STATE_CLOSING: | ||
print("WS closing") | ||
elif ws_socket.get_ready_state() == WebSocketPeer.STATE_CLOSED: | ||
print("WS closed") | ||
|
||
func _exit_tree(): | ||
ws_socket.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[SETTINGS] | ||
shortcut_kill_game = "ctrl+q" | ||
fullscreen = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import asyncio | ||
import websockets | ||
import keyboard | ||
import threading | ||
import configparser | ||
|
||
# List to hold connected WebSocket clients | ||
connected_clients = set() | ||
current_shortcut = None | ||
|
||
def read_config(): | ||
config = configparser.ConfigParser() | ||
config.read('launcher_config.ini') | ||
shortcut_kill_game = config.get('SETTINGS', 'shortcut_kill_game', fallback="ctrl+q") | ||
if shortcut_kill_game: | ||
# Strip any surrounding quotes | ||
shortcut_kill_game = shortcut_kill_game.strip('"\'') | ||
|
||
return shortcut_kill_game | ||
|
||
async def handle_client(websocket, path): | ||
# Register the client | ||
connected_clients.add(websocket) | ||
print(f"New client connected. Total clients: {len(connected_clients)}") | ||
try: | ||
# Keep the connection open and listen for messages (though we won't use them) | ||
async for _ in websocket: | ||
pass | ||
except websockets.exceptions.ConnectionClosed: | ||
print("Client disconnected") | ||
finally: | ||
# Unregister the client | ||
connected_clients.remove(websocket) | ||
print(f"Client disconnected. Total clients: {len(connected_clients)}") | ||
|
||
async def broadcast_key_press(key_name): | ||
if connected_clients: # Check if there are any connected clients | ||
message = f"KEY_PRESSED:{key_name}" | ||
print(f"Broadcasting message: {message}") | ||
await asyncio.gather(*[client.send(message) for client in connected_clients]) | ||
else: | ||
print("No clients connected; message not sent.") | ||
|
||
def on_shortcut_combination(): | ||
print(f"Shortcut {current_shortcut} triggered!") | ||
if connected_clients: | ||
asyncio.run_coroutine_threadsafe(broadcast_key_press(f"KEY_COMBINATION:{current_shortcut}"), loop) | ||
else: | ||
print("No clients to send the key combination to.") | ||
|
||
def start_websocket_server(): | ||
global loop | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
|
||
# Start the WebSocket server | ||
start_server = websockets.serve(handle_client, "localhost", 65432) | ||
|
||
# Run the server in the event loop | ||
loop.run_until_complete(start_server) | ||
print("WebSocket server started on ws://localhost:65432") | ||
loop.run_forever() | ||
|
||
def initialize_shortcut(): | ||
global current_shortcut | ||
current_shortcut = read_config() | ||
if current_shortcut: | ||
keyboard.add_hotkey(current_shortcut, on_shortcut_combination) | ||
print(f"Listening for the shortcut from config: {current_shortcut}") | ||
else: | ||
print("No shortcut found in config.") | ||
|
||
# Start the WebSocket server in a background thread | ||
server_thread = threading.Thread(target=start_websocket_server) | ||
server_thread.start() | ||
|
||
# Initialize the shortcut from config | ||
# Start listening for key presses | ||
initialize_shortcut() | ||
print("Listening for key presses...") |