Skip to content

Commit 5ac62a6

Browse files
committed
Merge pull request godotengine#97743 from bruvzg/has_kbd
[DisplayServer] Implement `has_hardware_keyboard` method for Android and iOS.
2 parents 6e03910 + 25f439c commit 5ac62a6

File tree

11 files changed

+56
-1
lines changed

11 files changed

+56
-1
lines changed

doc/classes/DisplayServer.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,13 @@
893893
Returns [code]true[/code] if the specified [param feature] is supported by the current [DisplayServer], [code]false[/code] otherwise.
894894
</description>
895895
</method>
896+
<method name="has_hardware_keyboard" qualifiers="const">
897+
<return type="bool" />
898+
<description>
899+
Returns [code]true[/code] if hardware keyboard is connected.
900+
[b]Note:[/b] This method is implemented on Android and iOS, on other platforms this method always returns [code]true[/code].
901+
</description>
902+
</method>
896903
<method name="help_set_search_callbacks">
897904
<return type="void" />
898905
<param index="0" name="search_callback" type="Callable" />

platform/android/display_server_android.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ int DisplayServerAndroid::virtual_keyboard_get_height() const {
304304
return godot_io_java->get_vk_height();
305305
}
306306

307+
bool DisplayServerAndroid::has_hardware_keyboard() const {
308+
GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
309+
ERR_FAIL_NULL_V(godot_io_java, false);
310+
311+
return godot_io_java->has_hardware_keyboard();
312+
}
313+
307314
void DisplayServerAndroid::window_set_window_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
308315
window_event_callback = p_callable;
309316
}

platform/android/display_server_android.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class DisplayServerAndroid : public DisplayServer {
138138
virtual void virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), VirtualKeyboardType p_type = KEYBOARD_TYPE_DEFAULT, int p_max_length = -1, int p_cursor_start = -1, int p_cursor_end = -1) override;
139139
virtual void virtual_keyboard_hide() override;
140140
virtual int virtual_keyboard_get_height() const override;
141+
virtual bool has_hardware_keyboard() const override;
141142

142143
virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
143144
virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;

platform/android/java/lib/src/org/godotengine/godot/GodotIO.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ public int[] getDisplayCutouts() {
216216
return result;
217217
}
218218

219+
public boolean hasHardwareKeyboard() {
220+
if (edit != null) {
221+
return edit.hasHardwareKeyboard();
222+
} else {
223+
return false;
224+
}
225+
}
226+
219227
public void showKeyboard(String p_existing_text, int p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
220228
if (edit != null) {
221229
edit.showKeyboard(p_existing_text, GodotEditText.VirtualKeyboardType.values()[p_type], p_max_input_length, p_cursor_start, p_cursor_end);

platform/android/java/lib/src/org/godotengine/godot/input/GodotEditText.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private boolean needHandlingInGodot(int keyCode, KeyEvent keyEvent) {
264264
isModifiedKey;
265265
}
266266

267-
boolean hasHardwareKeyboard() {
267+
public boolean hasHardwareKeyboard() {
268268
Configuration config = getResources().getConfiguration();
269269
boolean hasHardwareKeyboardConfig = config.keyboard != Configuration.KEYBOARD_NOKEYS &&
270270
config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;

platform/android/java_godot_io_wrapper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instanc
6363
_get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;");
6464
_show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;IIII)V");
6565
_hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
66+
_has_hardware_keyboard = p_env->GetMethodID(cls, "hasHardwareKeyboard", "()Z");
6667
_set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
6768
_get_screen_orientation = p_env->GetMethodID(cls, "getScreenOrientation", "()I");
6869
_get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(IZ)Ljava/lang/String;");
@@ -220,6 +221,16 @@ bool GodotIOJavaWrapper::has_vk() {
220221
return (_show_keyboard != nullptr) && (_hide_keyboard != nullptr);
221222
}
222223

224+
bool GodotIOJavaWrapper::has_hardware_keyboard() {
225+
if (_has_hardware_keyboard) {
226+
JNIEnv *env = get_jni_env();
227+
ERR_FAIL_NULL_V(env, false);
228+
return env->CallBooleanMethod(godot_io_instance, _has_hardware_keyboard);
229+
} else {
230+
return false;
231+
}
232+
}
233+
223234
void GodotIOJavaWrapper::show_vk(const String &p_existing, int p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
224235
if (_show_keyboard) {
225236
JNIEnv *env = get_jni_env();

platform/android/java_godot_io_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class GodotIOJavaWrapper {
5858
jmethodID _get_unique_id = 0;
5959
jmethodID _show_keyboard = 0;
6060
jmethodID _hide_keyboard = 0;
61+
jmethodID _has_hardware_keyboard = 0;
6162
jmethodID _set_screen_orientation = 0;
6263
jmethodID _get_screen_orientation = 0;
6364
jmethodID _get_system_dir = 0;
@@ -80,6 +81,7 @@ class GodotIOJavaWrapper {
8081
Rect2i get_display_safe_area();
8182
String get_unique_id();
8283
bool has_vk();
84+
bool has_hardware_keyboard();
8385
void show_vk(const String &p_existing, int p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end);
8486
void hide_vk();
8587
int get_vk_height();

platform/ios/display_server_ios.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ class DisplayServerIOS : public DisplayServer {
224224

225225
void virtual_keyboard_set_height(int height);
226226
virtual int virtual_keyboard_get_height() const override;
227+
virtual bool has_hardware_keyboard() const override;
227228

228229
virtual void clipboard_set(const String &p_text) override;
229230
virtual String clipboard_get() const override;

platform/ios/display_server_ios.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
#import <sys/utsname.h>
4747

48+
#import <GameController/GameController.h>
49+
4850
static const float kDisplayServerIOSAcceleration = 1.f;
4951

5052
DisplayServerIOS *DisplayServerIOS::get_singleton() {
@@ -756,6 +758,14 @@ _FORCE_INLINE_ int _convert_utf32_offset_to_utf16(const String &p_existing_text,
756758
return virtual_keyboard_height;
757759
}
758760

761+
bool DisplayServerIOS::has_hardware_keyboard() const {
762+
if (@available(iOS 14.0, *)) {
763+
return [GCKeyboard coalescedKeyboard];
764+
} else {
765+
return false;
766+
}
767+
}
768+
759769
void DisplayServerIOS::clipboard_set(const String &p_text) {
760770
[UIPasteboard generalPasteboard].string = [NSString stringWithUTF8String:p_text.utf8()];
761771
}

servers/display_server.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,10 @@ int DisplayServer::virtual_keyboard_get_height() const {
634634
ERR_FAIL_V_MSG(0, "Virtual keyboard not supported by this display server.");
635635
}
636636

637+
bool DisplayServer::has_hardware_keyboard() const {
638+
return true;
639+
}
640+
637641
void DisplayServer::cursor_set_shape(CursorShape p_shape) {
638642
WARN_PRINT("Cursor shape not supported by this display server.");
639643
}
@@ -976,6 +980,8 @@ void DisplayServer::_bind_methods() {
976980

977981
ClassDB::bind_method(D_METHOD("virtual_keyboard_get_height"), &DisplayServer::virtual_keyboard_get_height);
978982

983+
ClassDB::bind_method(D_METHOD("has_hardware_keyboard"), &DisplayServer::has_hardware_keyboard);
984+
979985
ClassDB::bind_method(D_METHOD("cursor_set_shape", "shape"), &DisplayServer::cursor_set_shape);
980986
ClassDB::bind_method(D_METHOD("cursor_get_shape"), &DisplayServer::cursor_get_shape);
981987
ClassDB::bind_method(D_METHOD("cursor_set_custom_image", "cursor", "shape", "hotspot"), &DisplayServer::cursor_set_custom_image, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));

0 commit comments

Comments
 (0)