Fix alt buttons and add hack to combat flickery key presses (far from perfect)

This commit is contained in:
Reece Mackie 2019-04-26 20:31:56 +01:00
parent 1f0115800c
commit 3ee97ceb4f
5 changed files with 108 additions and 13 deletions

View File

@ -58,7 +58,7 @@ void App::Update()
DisableCursor(); DisableCursor();
} }
if (IsKeyDown(KEY_LEFT_ALT)) //Unable to get working on my PC if (IsKeyDown(KEY_LEFT_ALT))
DrawRectangle(250, 250, 20, 20, BLACK); DrawRectangle(250, 250, 20, 20, BLACK);
if (IsKeyDown(KEY_BACKSPACE)) if (IsKeyDown(KEY_BACKSPACE))
DrawRectangle(280, 250, 20, 20, BLACK); DrawRectangle(280, 250, 20, 20, BLACK);

View File

@ -4,7 +4,12 @@
#include "pch.h" #include "pch.h"
//Define what header we use for BaseApp.h
#define PCH "pch.h" #define PCH "pch.h"
//Enable hold hack
#define HOLDHACK
#include "BaseApp.h" #include "BaseApp.h"
namespace raylibUWP namespace raylibUWP

View File

@ -8,6 +8,9 @@
* *
* #define PCH * #define PCH
* This defines what header is the PCH and needs to be included * 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) * 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 // Process Mouse
{ {
@ -372,25 +428,52 @@ protected:
void OnKeyDown(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) void OnKeyDown(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args)
{ {
UWPMessage* msg = CreateUWPMessage(); #ifdef HOLDHACK
msg->Type = RegisterKey; //Start the hack
msg->Int0 = (int)args->VirtualKey; KeyboardStateHack[(int)args->VirtualKey] = 1;
msg->Char0 = 1; #endif
UWPSendMessage(msg);
RegisterKey((int)args->VirtualKey, 1);
} }
void OnKeyUp(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) void OnKeyUp(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args)
{ {
//TODO: Fix hold errors #ifdef HOLDHACK
UWPMessage* msg = CreateUWPMessage(); //The same hack
msg->Type = RegisterKey; if (KeyboardStateHack[(int)args->VirtualKey] == 1)
msg->Int0 = (int)args->VirtualKey; {
msg->Char0 = 0; KeyboardStateHack[(int)args->VirtualKey] = 2;
UWPSendMessage(msg); }
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: 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) void MoveMouse(Vector2 pos)
{ {
CoreWindow ^window = CoreWindow::GetForCurrentThread(); CoreWindow ^window = CoreWindow::GetForCurrentThread();
@ -442,6 +525,10 @@ private:
int height = 480; int height = 480;
int CurrentPointerID = -1; 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 //Application source for creating the program

View File

@ -3220,6 +3220,9 @@ static void PollInputEvents(void)
if (actualKey > -1) if (actualKey > -1)
currentKeyState[actualKey] = msg->Char0; currentKeyState[actualKey] = msg->Char0;
if (msg->Char0)
msg->Char0 = 2;
break; break;
} }

View File

@ -205,7 +205,7 @@ static int android_close(void *cookie)
#if defined(PLATFORM_UWP) #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 int UWPOutMessageId = -1; //Stores the last index for the message
static UWPMessage* UWPOutMessages[MAX_MESSAGES]; //Messages out to UWP static UWPMessage* UWPOutMessages[MAX_MESSAGES]; //Messages out to UWP