Created
August 18, 2022 05:24
-
-
Save nisovin/52792acc2ab0d74eb273a8294c955076 to your computer and use it in GitHub Desktop.
Godot code for getting mouse and keyboard input while window is not focused
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
using Godot; | |
using System; | |
using System.Collections.Generic; | |
using System.Runtime.InteropServices; | |
public class GlobalInput : Godot.Node | |
{ | |
private Dictionary<int, string> keyWatches = new Dictionary<int, string>(); | |
private Dictionary<int, bool> keyStates = new Dictionary<int, bool>(); | |
[Signal] | |
delegate void key_event(string key, bool pressed); | |
public override void _Process(float delta) { | |
foreach (int keyCode in keyWatches.Keys) { | |
bool state = IsKeyPressed(keyCode); | |
if (state != keyStates[keyCode]) { | |
keyStates[keyCode] = state; | |
EmitSignal("key_event", keyWatches[keyCode], state); | |
} | |
} | |
} | |
public void watch_key_raw(int key_code, string key_name) { | |
if (!keyWatches.ContainsKey(key_code)) { | |
keyWatches.Add(key_code, key_name); | |
keyStates.Add(key_code, false); | |
} | |
} | |
public Vector2 get_mouse_position() { | |
return GetCursorPosition(); | |
} | |
public bool is_mouse_button_pressed(int button_index) { | |
if (!buttonMap.ContainsKey(button_index)) return false; | |
return IsKeyPressed(buttonMap[button_index]); | |
} | |
public bool is_key_pressed(int scancode) { | |
return false; | |
} | |
public bool is_key_pressed_raw(int key_code) { | |
return IsKeyPressed(key_code); | |
// https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes | |
} | |
static Dictionary<int, int> buttonMap = new Dictionary<int, int>() { | |
{(int)ButtonList.Left, 0x1}, | |
{(int)ButtonList.Right, 0x2}, | |
{(int)ButtonList.Middle, 0x4}, | |
{(int)ButtonList.Xbutton1, 0x5}, | |
{(int)ButtonList.Xbutton2, 0x6} | |
}; | |
[StructLayout(LayoutKind.Sequential)] | |
public struct POINT | |
{ | |
public int X; | |
public int Y; | |
public static implicit operator Vector2(POINT point) | |
{ | |
return new Vector2(point.X, point.Y); | |
} | |
} | |
[DllImport("user32.dll")] | |
public static extern bool GetCursorPos(out POINT lpPoint); | |
[DllImport("user32.dll")] | |
public static extern short GetAsyncKeyState(UInt16 virtualKeyCode); | |
public static Vector2 GetCursorPosition() | |
{ | |
POINT lpPoint; | |
GetCursorPos(out lpPoint); | |
return lpPoint; | |
} | |
public static bool IsKeyPressed(int code) { | |
return GetAsyncKeyState((UInt16)code) != 0; | |
} | |
// Source: https://stackoverflow.com/questions/1316681/getting-mouse-position-in-c-sharp | |
} |
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
extends Node | |
# This is used by setting the GlobalInput C# script as a singleton | |
func _ready(): | |
GlobalInput.watch_key_raw(0xA0, "shift_left") | |
GlobalInput.connect("key_event", self, "_on_key") | |
func _process(delta: float) -> void: | |
$Label.text = var2str(GlobalInput.get_mouse_position()) | |
$Label2.text = "yes" if GlobalInput.is_mouse_button_pressed(BUTTON_LEFT) else "no" | |
var shift = GlobalInput.is_key_pressed_raw(0x10) | |
var left_shift = GlobalInput.is_key_pressed_raw(0xA0) | |
var right_shift = GlobalInput.is_key_pressed_raw(0xA1) | |
$Label3.text = ("yes " if shift else "no ") + ("left" if left_shift else "") + ("right" if right_shift else "") | |
func _on_key(key, pressed): | |
print("KEY! ", key, pressed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment