Skip to content

Commit 1c883e8

Browse files
(finally) managed to get a working way to check caps lock
1 parent e8ba9a8 commit 1c883e8

File tree

5 files changed

+74
-18
lines changed

5 files changed

+74
-18
lines changed

source/InitState.hx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class InitState extends FlxState
7474
FlxG.autoPause = false;
7575

7676
// Set the stage and scaling modes
77-
Lib.current.stage.align = "tl";
77+
Lib.current.stage.align = 'tl';
7878
Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
7979

8080
// Disable the binds for increasing/decreasing/muting
@@ -91,7 +91,7 @@ class InitState extends FlxState
9191

9292
// Disable the right-click context menu for HTML5
9393
#if html5
94-
Browser.document.addEventListener("contextmenu", (e) ->
94+
Browser.document.addEventListener('contextmenu', (e) ->
9595
{
9696
e.preventDefault();
9797
});
@@ -103,6 +103,19 @@ class InitState extends FlxState
103103
CacheUtil.vcrMario85Shader = new VCRMario85Shader();
104104
#end
105105
CacheUtil.grainShader = new GrainShader();
106+
107+
// Configure the event listener for detecting caps lock
108+
// if the target is set to HTML5
109+
#if html5
110+
untyped __js__('
111+
window.__haxe_capslock__ = false;
112+
document.addEventListener("keydown", function (e) {
113+
if (e.getModifierState) {
114+
window.__haxe_capslock__ = e.getModifierState("CapsLock");
115+
}
116+
});
117+
');
118+
#end
106119
}
107120

108121
function addBackgroundProcesses():Void
@@ -172,15 +185,6 @@ class InitState extends FlxState
172185
});
173186
#end
174187

175-
// Enable/disable caps lock
176-
FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, (_) ->
177-
{
178-
if (FlxG.keys.justPressed.CAPSLOCK)
179-
{
180-
CacheUtil.capsLockEnabled = !CacheUtil.capsLockEnabled;
181-
}
182-
});
183-
184188
// Do shit like saving the user's data when the game closes
185189
Application.current.window.onClose.add(() ->
186190
{

source/starcore/backend/util/CacheUtil.hx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,5 @@ final class CacheUtil
5555
*/
5656
public static var grainShader:GrainShader;
5757

58-
/**
59-
* Is caps lock enabled?
60-
*/
61-
public static var capsLockEnabled:Bool = false; // TODO: Find a better way to do this...
62-
6358
function new() {}
6459
}

source/starcore/backend/util/FlixelUtil.hx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ import flixel.sound.filters.effects.FlxSoundReverbEffect;
2323
import js.Browser;
2424
#end
2525

26+
// Directly inject C++ code for
27+
// detecting if caps lock is enabled
28+
// on Windows/C++ builds
29+
#if cpp
30+
@:headerCode('
31+
extern "C" bool hx_isCapsLockOn();
32+
')
33+
@:cppFileCode('
34+
#include <windows.h>
35+
extern "C" bool hx_isCapsLockOn() {
36+
return (GetKeyState(VK_CAPITAL) & 0x0001) != 0;
37+
}
38+
')
39+
#end
40+
2641
/**
2742
* Utility class for handling objects and components specific to Flixel.
2843
*/
@@ -393,6 +408,47 @@ final class FlixelUtil
393408
}
394409
}
395410

411+
/**
412+
* Gets if caps lock is enabled.
413+
* Only works on HTML5, Windows/C++, macOS and Linux builds.
414+
*
415+
* @return If caps lock is enabled.
416+
*/
417+
public static inline function getCapsLockedEnabled():Bool
418+
{
419+
#if html5
420+
return untyped __js__('window.__haxe_capslock__');
421+
#elseif cpp // This includes Windows builds
422+
return untyped __cpp__('hx_isCapsLockOn()');
423+
#elseif mac
424+
try
425+
{
426+
var proc = new sys.io.Process("bash", ["-c", "osascript -e 'tell application \"System Events\" to get caps lock'"]);
427+
var result = proc.stdout.readLine();
428+
proc.close();
429+
return result == "true";
430+
}
431+
catch (e:Dynamic)
432+
{
433+
return false;
434+
}
435+
#elseif linux
436+
try
437+
{
438+
var proc = new sys.io.Process("bash", ["-c", "xset q | grep Caps"]);
439+
var output = proc.stdout.readAll().toString();
440+
proc.close();
441+
return output.indexOf("on") != -1;
442+
}
443+
catch (e:Dynamic)
444+
{
445+
return false;
446+
}
447+
#else
448+
return false;
449+
#end
450+
}
451+
396452
/**
397453
* Tweens an `FlxTypedGroup<FlxSprite>`'s members with ease.
398454
*

source/starcore/menus/MainMenuState.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package starcore.menus;
22

3+
import starcore.ui.TextBox;
34
import starcore.play.PlayState;
45
import flixel.FlxG;
56
import flixel.FlxSprite;

source/starcore/ui/TextBox.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import flixel.util.FlxColor;
99
import flixel.util.FlxTimer;
1010
import openfl.ui.MouseCursor;
1111
import starcore.backend.data.Constants;
12-
import starcore.backend.util.CacheUtil;
1312
import starcore.backend.util.DataUtil;
1413
import starcore.backend.util.FlixelUtil;
1514

@@ -106,6 +105,7 @@ class TextBox extends FlxSpriteGroup
106105
displayText = new FlxText();
107106
displayText.text = textHint;
108107
displayText.color = FlxColor.BLACK;
108+
trace(font);
109109
displayText.font = font;
110110
displayText.size = size;
111111
displayText.fieldWidth = width;
@@ -208,7 +208,7 @@ class TextBox extends FlxSpriteGroup
208208
{
209209
if (currentJustPressedKeys.contains(key) || canHoldLetter)
210210
{
211-
var shift:Bool = ((!CacheUtil.capsLockEnabled) ? FlxG.keys.pressed.SHIFT : !FlxG.keys.pressed.SHIFT);
211+
var shift:Bool = ((!FlixelUtil.getCapsLockedEnabled()) ? FlxG.keys.pressed.SHIFT : !FlxG.keys.pressed.SHIFT);
212212
if (type != STRING && shift)
213213
{
214214
// Prevent the user from adding a new character

0 commit comments

Comments
 (0)