Skip to content

Instantly share code, notes, and snippets.

@leezer3
Last active October 26, 2020 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leezer3/0a9106a3f612c48ec7b33d853176e3a3 to your computer and use it in GitHub Desktop.
Save leezer3/0a9106a3f612c48ec7b33d853176e3a3 to your computer and use it in GitHub Desktop.
using System;
using Microsoft.Win32;
using OpenTK;
using OpenTK.Input;
namespace TestProgram
{
class Program
{
static void Main(string[] args)
{
ToolkitOptions options = new ToolkitOptions();
options.Backend = PlatformBackend.PreferX11;
Toolkit.Init(options);
TestWindow window = new TestWindow();
window.Run();
}
}
class TestWindow : GameWindow
{
internal TestWindow() : base()
{
KeyUp += KeyUpEvent;
KeyDown += KeyDownEvent;
SystemEvents.SessionSwitch += (SessionSwitch);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
KeyboardState state = Keyboard.GetState();
}
internal void KeyDownEvent(object sender, KeyboardKeyEventArgs keyboardKeyEventArgs)
{
Console.WriteLine(keyboardKeyEventArgs.Key + " pressed");
}
internal void KeyUpEvent(object sender, KeyboardKeyEventArgs keyboardKeyEventArgs)
{
Console.WriteLine(keyboardKeyEventArgs.Key + " released");
}
private void SessionSwitch(object sender, SessionSwitchEventArgs e)
{
KeyboardState state = Keyboard.GetState();
if (e.Reason == SessionSwitchReason.SessionLock)
{
if (state.IsAnyKeyDown)
{
Console.WriteLine("Locked, the following keys are down:");
for (Key key = 0; key < Key.LastKey; key++)
{
if (state.IsKeyDown(key))
{
Console.WriteLine(key + " is down");
}
}
}
}
if (e.Reason == SessionSwitchReason.SessionUnlock)
{
Console.WriteLine("Unlocked, the following keys are down:");
for (Key key = 0; key < Key.LastKey; key++)
{
if (state.IsKeyDown(key))
{
Console.WriteLine(key + " is down");
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment