Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pressing keys on a hardware keyboard showing the software keyboard #5274

Merged
merged 4 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion osu.Framework.Android/AndroidGameView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public bool PointerCapture

private readonly Game game;

private InputMethodManager inputMethodManager;

/// <summary>
/// Whether <see cref="AndroidTextInput"/> is active.
/// </summary>
private bool textInputActive;

public AndroidGameView(AndroidGameActivity activity, Game game)
: base(activity)
{
Expand Down Expand Up @@ -97,6 +104,8 @@ private void init()

// disable ugly green border when view is focused via hardware keyboard/mouse.
DefaultFocusHighlightEnabled = false;

inputMethodManager = Activity.GetSystemService(Context.InputMethodService) as InputMethodManager;
}

protected override void CreateFrameBuffer()
Expand Down Expand Up @@ -238,15 +247,43 @@ private void updateSafeArea()
};
}

public override bool OnCheckIsTextEditor() => true;
public override bool OnCheckIsTextEditor() => textInputActive;

/// <returns><c>null</c> to disable input methods</returns>
public override IInputConnection OnCreateInputConnection(EditorInfo outAttrs)
{
// Properly disable native input methods so that the software keyboard doesn't unexpectedly open.
// Eg. when pressing keys on a hardware keyboard.
if (!textInputActive)
return null;

outAttrs.ImeOptions = ImeFlags.NoExtractUi | ImeFlags.NoFullscreen;
outAttrs.InputType = InputTypes.TextVariationVisiblePassword | InputTypes.TextFlagNoSuggestions;
return new AndroidInputConnection(this, true);
}

internal void StartTextInput()
{
textInputActive = true;
Activity.RunOnUiThread(() =>
{
inputMethodManager.RestartInput(this); // this syncs the Android input method state with `OnCreateInputConnection()`.
RequestFocus();
inputMethodManager?.ShowSoftInput(this, 0);
});
}

internal void StopTextInput()
{
textInputActive = false;
Activity.RunOnUiThread(() =>
{
inputMethodManager.RestartInput(this);
inputMethodManager?.HideSoftInputFromWindow(WindowToken, HideSoftInputFlags.None);
ClearFocus();
});
}

public override void SwapBuffers()
{
try
Expand Down
28 changes: 4 additions & 24 deletions osu.Framework.Android/Input/AndroidTextInput.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using Android.Content;
using Android.Views;
using Android.Views.InputMethods;
using osu.Framework.Input;

namespace osu.Framework.Android.Input
{
public class AndroidTextInput : TextInputSource
internal class AndroidTextInput : TextInputSource
{
private readonly AndroidGameView view;
private readonly InputMethodManager inputMethodManager;

public AndroidTextInput(AndroidGameView view)
{
this.view = view;
inputMethodManager = view.Activity.GetSystemService(Context.InputMethodService) as InputMethodManager;
}

private void commitText(string text)
Expand All @@ -36,33 +30,19 @@ protected override void ActivateTextInput(bool allowIme)
{
view.KeyDown += keyDown;
view.CommitText += commitText;

view.Activity.RunOnUiThread(() =>
{
view.RequestFocus();
inputMethodManager?.ShowSoftInput(view, 0);
});
view.StartTextInput();
}

protected override void EnsureTextInputActivated(bool allowIme)
{
view.Activity.RunOnUiThread(() =>
{
view.RequestFocus();
inputMethodManager?.ShowSoftInput(view, 0);
});
view.StartTextInput();
}

protected override void DeactivateTextInput()
{
view.KeyDown -= keyDown;
view.CommitText -= commitText;

view.Activity.RunOnUiThread(() =>
{
inputMethodManager?.HideSoftInputFromWindow(view.WindowToken, HideSoftInputFlags.None);
view.ClearFocus();
});
view.StopTextInput();
}
}
}