Skip to content

Commit 1ff7dba

Browse files
committed
Keyboard WIP
1 parent fd5d468 commit 1ff7dba

6 files changed

Lines changed: 79 additions & 25 deletions

File tree

Plugins/CS/Keyboard.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Runtime.InteropServices;
5+
using AOT;
6+
using UnityEngine;
7+
8+
namespace iOSNativePlugin
9+
{
10+
public static class Keyboard
11+
{
12+
[DllImport("__Internal")] static extern void Keyboard_RegisterKeyPressCallback(LongCallback callback);
13+
[DllImport("__Internal")] static extern void Keyboard_RegisterKeyReleaseCallback(LongCallback callback);
14+
[DllImport("__Internal")] static extern bool Keyboard_IsKeyboardSupported();
15+
[DllImport("__Internal")] static extern bool Keyboard_IsAnyKeyPressed();
16+
17+
public static event Action<KeyCode> OnKeyPressed;
18+
public static event Action<KeyCode> OnKeyReleased;
19+
20+
static void Init()
21+
{
22+
23+
}
24+
25+
[MonoPInvokeCallback(typeof(LongCallback))]
26+
static void OnKeyPressedCallback(long GCKeyCode)
27+
{
28+
OnKeyPressed?.Invoke();
29+
}
30+
31+
[MonoPInvokeCallback(typeof(LongCallback))]
32+
static void OnKeyReleasedCallback(long GCKeyCode)
33+
{
34+
OnKeyReleased?.Invoke();
35+
}
36+
37+
static KeyCode GetKeyCodeFromGCKeyCode(long GCKeyCode)
38+
{
39+
return GCKeyCode switch
40+
{
41+
42+
}
43+
}
44+
}
45+
}

Plugins/CS/Keyboard.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "../Utils.mm"
22
#import <GameController/GameController.h>
3-
@interface Input : NSObject
3+
@interface Keyboard : NSObject
4+
45

56
@end
Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
1-
#import "../Headers/Input.h"
1+
#import "../Headers/Keyboard.h"
22

33

4-
@implementation Input
4+
@implementation Keyboard
55
static BOOL inited;
66
API_AVAILABLE(ios(14.0)) static GCKeyboard* keyboard;
7-
NSMutableSet *pressedKeys;
7+
static LongCallback OnKeyPressedCallback;
8+
static LongCallback OnKeyReleasedCallback;
89

910
+(void)Init
1011
{
1112
if(inited)
1213
return;
1314
if (@available(iOS 14.0, *)) {
1415
keyboard = GCKeyboard.coalescedKeyboard;
15-
pressedKeys = [NSMutableSet init];
16-
[[NSNotificationCenter defaultCenter] addObserver:[Input class]
16+
[[NSNotificationCenter defaultCenter] addObserver:[Keyboard class]
1717
selector:@selector(OnKeyboardConnected:)
1818
name:GCKeyboardDidConnectNotification
1919
object:nil];
20-
[[NSNotificationCenter defaultCenter] addObserver:[Input class]
20+
[[NSNotificationCenter defaultCenter] addObserver:[Keyboard class]
2121
selector:@selector(OnKeyboardDisconnected:)
2222
name:GCKeyboardDidDisconnectNotification
2323
object:nil];
2424
}
2525
inited = YES;
2626
}
27+
+(void)RegisterKeyPressCallback:(LongCallback)callback
28+
{
29+
OnKeyPressedCallback = callback;
30+
}
31+
32+
+(void)RegisterKeyReleaseCallback:(LongCallback)callback
33+
{
34+
OnKeyReleasedCallback = callback;
35+
}
2736

2837
+(void)OnKeyboardConnected:(NSNotification *)notification
2938
{
3039
if (@available(iOS 14.0, *)) {
3140
keyboard = notification.object;
3241
keyboard.keyboardInput.keyChangedHandler = ^(GCKeyboardInput * _Nonnull keyboard, GCControllerButtonInput * _Nonnull key, GCKeyCode keyCode, BOOL pressed)
3342
{
34-
NSNumber *KeyCodeNumber = [NSNumber numberWithLong:keyCode];
3543
if(pressed){
36-
[pressedKeys addObject:KeyCodeNumber];
44+
if(OnKeyPressedCallback)
45+
OnKeyPressedCallback(keyCode);
3746
}
3847
else{
39-
[pressedKeys removeObject:KeyCodeNumber];
48+
if(OnKeyReleasedCallback)
49+
OnKeyReleasedCallback(keyCode);
4050
}
4151
};
4252
}
@@ -57,35 +67,22 @@ +(BOOL)IsKeyboardSupported
5767
{
5868
if (@available(iOS 14.0, *))
5969
{
60-
[Input Init];
70+
[Keyboard Init];
6171
return keyboard != nil;
6272
} else {
6373
return NO;
6474
}
65-
6675
}
6776

6877
+(BOOL)IsAnyKeyPressed
6978
{
7079
if (@available(iOS 14.0, *))
7180
{
72-
[Input Init];
81+
[Keyboard Init];
7382
return keyboard.keyboardInput.anyKeyPressed;
7483
} else {
7584
return false;
7685
}
7786
}
7887

79-
+(BOOL)GetKey:(GCKeyCode)keyCode
80-
API_AVAILABLE(ios(14.0))
81-
{
82-
if (@available(iOS 14.0, *)){
83-
[Input Init];
84-
NSNumber *KeyCodeNumber = [NSNumber numberWithLong:keyCode];
85-
return [pressedKeys containsObject:KeyCodeNumber];
86-
}
87-
return NO;
88-
89-
}
90-
9188
@end
File renamed without changes.

0 commit comments

Comments
 (0)