From 3ee97ceb4fcc6adfe59ca6580c4d5379d1b59d29 Mon Sep 17 00:00:00 2001 From: Reece Mackie <20544390+Rover656@users.noreply.github.com> Date: Fri, 26 Apr 2019 20:31:56 +0100 Subject: [PATCH] Fix alt buttons and add hack to combat flickery key presses (far from perfect) --- projects/VS2017.UWP/raylib.App.UWP/App.cpp | 2 +- projects/VS2017.UWP/raylib.App.UWP/App.h | 5 + projects/VS2017.UWP/raylib.App.UWP/BaseApp.h | 109 +++++++++++++++++-- src/core.c | 3 + src/utils.c | 2 +- 5 files changed, 108 insertions(+), 13 deletions(-) diff --git a/projects/VS2017.UWP/raylib.App.UWP/App.cpp b/projects/VS2017.UWP/raylib.App.UWP/App.cpp index 1e5ff6466..520d5d7f4 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/App.cpp +++ b/projects/VS2017.UWP/raylib.App.UWP/App.cpp @@ -58,7 +58,7 @@ void App::Update() DisableCursor(); } - if (IsKeyDown(KEY_LEFT_ALT)) //Unable to get working on my PC + if (IsKeyDown(KEY_LEFT_ALT)) DrawRectangle(250, 250, 20, 20, BLACK); if (IsKeyDown(KEY_BACKSPACE)) DrawRectangle(280, 250, 20, 20, BLACK); diff --git a/projects/VS2017.UWP/raylib.App.UWP/App.h b/projects/VS2017.UWP/raylib.App.UWP/App.h index edd21f263..26c1d400b 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/App.h +++ b/projects/VS2017.UWP/raylib.App.UWP/App.h @@ -4,7 +4,12 @@ #include "pch.h" +//Define what header we use for BaseApp.h #define PCH "pch.h" + +//Enable hold hack +#define HOLDHACK + #include "BaseApp.h" namespace raylibUWP diff --git a/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h b/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h index 97a8097bc..2d4cc0a02 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h +++ b/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h @@ -8,6 +8,9 @@ * * #define PCH * This defines what header is the PCH and needs to be included +* +* #define HOLDHACK +* This enables a hack to fix flickering key presses (Temporary) * * Copyright (c) 2013-2019 Ramon Santamaria (@raysan5) * @@ -200,6 +203,59 @@ protected: } } + // Process Keyboard + { + for (int k = 0x08; k < 0xA6; k++) { + auto state = CoreWindow::GetForCurrentThread()->GetKeyState((Windows::System::VirtualKey) k); + +#ifdef HOLDHACK + //Super hacky way of waiting three frames to see if we are ready to register the key as deregistered + //This will wait an entire 4 frames before deregistering the key, this makes sure that the key is not flickering + if (KeyboardStateHack[k] == 2) + { + if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None) + { + KeyboardStateHack[k] = 3; + } + } + else if (KeyboardStateHack[k] == 3) + { + if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None) + { + KeyboardStateHack[k] = 4; + } + } + else if (KeyboardStateHack[k] == 4) + { + if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None) + { + //Reset key... + KeyboardStateHack[k] = 0; + + //Tell core + RegisterKey(k, 0); + } + } +#endif + //Left and right alt, KeyUp and KeyDown are not called for it + //No need to hack because this is not a character + + //TODO: Maybe do all other key registrations like this, no more key events? + + if (k == 0xA4 || k == 0xA5) + { + if ((state & CoreVirtualKeyStates::Down) == CoreVirtualKeyStates::Down) + { + RegisterKey(k, 1); + } + else + { + RegisterKey(k, 0); + } + } + } + } + // Process Mouse { @@ -372,25 +428,52 @@ protected: void OnKeyDown(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) { - UWPMessage* msg = CreateUWPMessage(); - msg->Type = RegisterKey; - msg->Int0 = (int)args->VirtualKey; - msg->Char0 = 1; - UWPSendMessage(msg); +#ifdef HOLDHACK + //Start the hack + KeyboardStateHack[(int)args->VirtualKey] = 1; +#endif + + RegisterKey((int)args->VirtualKey, 1); } void OnKeyUp(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) { - //TODO: Fix hold errors - UWPMessage* msg = CreateUWPMessage(); - msg->Type = RegisterKey; - msg->Int0 = (int)args->VirtualKey; - msg->Char0 = 0; - UWPSendMessage(msg); +#ifdef HOLDHACK + //The same hack + if (KeyboardStateHack[(int)args->VirtualKey] == 1) + { + KeyboardStateHack[(int)args->VirtualKey] = 2; + } + else if (KeyboardStateHack[(int)args->VirtualKey] == 2) + { + KeyboardStateHack[(int)args->VirtualKey] = 3; + } + else if (KeyboardStateHack[(int)args->VirtualKey] == 3) + { + KeyboardStateHack[(int)args->VirtualKey] = 4; + } + else if (KeyboardStateHack[(int)args->VirtualKey] == 4) + { + RegisterKey((int)args->VirtualKey, 0); + KeyboardStateHack[(int)args->VirtualKey] = 0; + } +#else + //No hack, allow flickers + RegisterKey((int)args->VirtualKey, 0); +#endif } private: + void RegisterKey(int key, char status) + { + UWPMessage* msg = CreateUWPMessage(); + msg->Type = UWPMessageType::RegisterKey; + msg->Int0 = key; + msg->Char0 = status; + UWPSendMessage(msg); + } + void MoveMouse(Vector2 pos) { CoreWindow ^window = CoreWindow::GetForCurrentThread(); @@ -442,6 +525,10 @@ private: int height = 480; int CurrentPointerID = -1; + +#ifdef HOLDHACK + char KeyboardStateHack[0xA6]; //0xA6 because the highest key we compare against is 0xA5 +#endif }; //Application source for creating the program diff --git a/src/core.c b/src/core.c index ff43ae08d..df4ad75fa 100644 --- a/src/core.c +++ b/src/core.c @@ -3220,6 +3220,9 @@ static void PollInputEvents(void) if (actualKey > -1) currentKeyState[actualKey] = msg->Char0; + + if (msg->Char0) + msg->Char0 = 2; break; } diff --git a/src/utils.c b/src/utils.c index 1f0953be9..dcdffaa66 100644 --- a/src/utils.c +++ b/src/utils.c @@ -205,7 +205,7 @@ static int android_close(void *cookie) #if defined(PLATFORM_UWP) -#define MAX_MESSAGES 128 //If there are over 128 messages, I will cry... either way, this may be too much +#define MAX_MESSAGES 512 //If there are over 128 messages, I will cry... either way, this may be too much EDIT: Welp, 512 static int UWPOutMessageId = -1; //Stores the last index for the message static UWPMessage* UWPOutMessages[MAX_MESSAGES]; //Messages out to UWP