Detecting Keypresses in iOS from On Screen and/or External Keyboard - c#

Is there any possible way to detect ALL key presses on the iOS on screen keyboard and/or on an external keyboard like these? I know the "accepted" way to catch typed text is to use an invisible textbox, but that seems to only catch the "text", not actual key presses (no Ctrl, Alts, Shift, etc.)
I can't find anything in the docs that suggests this is possible, though it seems pretty basic. Am I missing something, or can we really not achieve this in iOS?
Thanks!

You can override the keyCommands in ViewController to add the trigger action:
For example:
//MyViewController
override var keyCommands: [UIKeyCommand]?{
//Get UIKeyCommand
var keys = [UIKeyCommand]()
//Add triggle for Num only
for num in "123456789".characters {
keys.append(
UIKeyCommand(input:String(num),modifierFlags:
UIKeyModifierFlags(rawValue: 0), action:
#selector(MyViewController.keyCommand(_:)), discoverabilityTitle:
NSLocalizedString("keydiscover_number",
comment: "KeyDiscover Number")))
}
}
//The Triggle
func keyCommand(_ sender: UIKeyCommand) {
print("Number Clicked");
}
https://developer.apple.com/documentation/uikit/uiresponder/1621141-keycommands

Related

Is it possible to call a specific method every time a button is pressed, even when the console is not expecting user input?

I'm making a program for differentiating functions and want to call a specific method as soon as "esc" is pressed.
So far I've tried finding an answer through Google, but I can't seem to find anything that works. Does anyone know if this is possible in a C# console application?
There is no event to listen for key presses. Only Console.ReadKey. But you can turn it into a callback using Tasks. Like so:
public static void RegisterKeyHandler(Action<ConsoleKeyInfo> handler)
{
Task.Run(() =>
{
while (true)
{
var key = Console.ReadKey();
handler(key);
}
});
}
This only receives keys while the console window is focused.

Unity3D KeyCode.Home issue?

Hello i have a line of code that should quit the game upon pressing the three hardware button on the bottom like Menu, Home, Back
it seems that the back and the home only is working but the menu is not working
I have a Xiaomi Redmi Note 4X phone, how am i suppose to fix this issue?
Is it the same "Menu" as the other phone where when you press it, it'll show list of the opened app
here's the code:
if (Input.GetKey (KeyCode.Home) || Input.GetKey (KeyCode.Escape) || Input.GetKey (KeyCode.Menu))
{
Save ();
System.Diagnostics.Process.GetCurrentProcess ().Kill ();
}
Is it the same "Menu" as the other phone where when you press it,
it'll show list of the opened ap
Yes.
I have a Xiaomi Redmi Note 4X phone, how am i suppose to fix this
issue?
It should work otherwise it is a bug and you should file for a bug request. Before you file for abug request, use the script below to make that the Menu key is not mapped to another key.
public class KeyCodeFinder : MonoBehaviour
{
public Text text;
Array allKeyCodes;
void Start()
{
allKeyCodes = System.Enum.GetValues(typeof(KeyCode));
}
void Update()
{
foreach (KeyCode tempKey in allKeyCodes)
{
if (Input.GetKeyDown(tempKey))
{
text.text = "Pressed: KeyCode." + tempKey;
Debug.Log("Pressed: KeyCode." + tempKey);
}
}
}
}
Meanwhile, you can make a Java plugin that calls your C# function when the Menu keycode is pressed. That's another option you have.
Those buttons are OS related, you don't really control what happens with those. Well, you can track them and kill your app but this is going against what every single application does. That is, if you press the back or home or quit, it gets back to the main OS page, but your app is still on the stack ready to get back.
If you want to save when the user quits, maybe you want to look into
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html
This will make your app behave in a more common way.
On a side note, I would even assume this kind of attempt to over control the app would be rejected for AppStore (iOS) as it would not comply with their generic requirements.
It is allways possible to map a custom KeyCode that is not mapped so far.
First find out what keycode the key has
Event e = Event.current;
if (e.isKey)
{
Debug.Log("Detected key code: " + e.keyCode);
}
Let's say this gives you 10 as keycode (a one that is not mapped in Unities KeyCode so far)
You can than simply define a new one like
KeyCode MY_HOME_BUTTON = (KeyCode) 10;
and than use it like
if(Input.MY_HOME_BUTTON)
{
...
}
If it is completely possible to catch the home button depends on the OS (most of the time those buttons cannot be catched by an app but are handled by the OS first)

SendInput events don't get processed in specific UI area of game

I have a C# application which sends keyboard and mouse events to a game. In the game there's a small UI portion which can change based on what we want to see (like GPS map, different actions to take etc). Sometimes in this portion of the UI we have to press 'Enter' (or another key depending on how the user configured the game's keys) to activate something (for example, press Enter to activate Rescue service).
Now , the problem: all the key events sent from the c# app are processed fine OUTSIDE of this UI, except this 'activate' thing. At first I thought there's a problem with the 'ENTER' (Return) key event, but after chaning it to another key that was working in-game, I noticed that key is also ignored.
I am not sure if it's a problem related to the way I send the messages (maybe it needs to have a specific parameter) and it can be fixed by modifying my app, or is some blockage in-game - and in this case there's nothing I can do.
Here's the code I use to send the key events:
public static void PressKey(short key)
{
INPUT[] inputs = new INPUT[]
{
new INPUT
{
type = INPUT_KEYBOARD,
u = new InputUnion
{
ki = new KEYBDINPUT
{
wVk = (ushort)0,
wScan = (ushort)key,
dwFlags = KEYEVENTF_SCANCODE,
dwExtraInfo = IntPtr.Zero,
}
}
},
new INPUT
{
type = INPUT_KEYBOARD,
u = new InputUnion
{
ki = new KEYBDINPUT
{
wVk = (ushort)0,
wScan = (ushort)key,
dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP,
dwExtraInfo = GetMessageExtraInfo(),
}
}
}
};
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
}
But hoping its the 1st case, I would appreciate any help with this.
Added a screenshot in order to try to better explain the issue
(A) -> That is the UI window that requires "ENTER" to be pressed. Let's suppose that I modify the settings of the game to have "E" instead of "Enter" for the ACTIVATE action. Then the text "Call Assistance Service (ENTER)" will now show "Call Assistance Service (E)". By default, the "E" key is engine start in the game. And it works just fine (in the case (B)) . As soon as I change E to be 'Activate' , it's being completely ignored.
Another point which might (or might not) be relevant , is that the F-keys that can be at the bottom of the UI Window, also WORK just fine.
I hope this clears up the things a little bit more.
I have found what the issue was, and I'm pretty sure it's related to how the game handles the input (but since it's not me who coded the game I cannot guarantee it).
Anyway, to fix my issue, a delay needs to be added between the KEYDOWN and KEYUP events. For the time being I'm using a Timer with a callback for that, but probably there are other ways to do it also (better or not).
Hope this will help someone someday :)

How to detect if any key is pressed

How can I detect if any keyboard key is currently being pressed? I'm not interested in what the key is, I just want to know if any key is still pressed down.
if (Keyboard.IsKeyDown(Key.ANYKEY??)
{
}
public static IEnumerable<Key> KeysDown()
{
foreach (Key key in Enum.GetValues(typeof(Key)))
{
if (Keyboard.IsKeyDown(key))
yield return key;
}
}
you could then do:
if(KeysDown().Any()) //...
If you want to detect key pressed only in our application (when your WPF window is activated) add KeyDown like below:
public MainWindow()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
}
void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("You pressed a keyboard key.");
}
If you want to detect when a key is pressed even your WPF window is not active is a little harder but posibile. I recomend RegisterHotKey (Defines a system-wide hot key) and UnregisterHotKey from Windows API. Try using these in C# from pinvoke.net or these tutorials:
Global Hotkeys: Register a hotkey that is triggered even when form isn't focused.
Simple steps to enable Hotkey and ShortcutInput user control
Thse is a sample in Microsoft Forums.
You will use Virtual-Key Codes.
I Hope that I was clear and you will understand my answer.
Iterate over the System.Windows.Input.Key enum values.
public static bool IsAnyKeyDown()
{
var values = Enum.GetValues(typeof(Key));
foreach (var v in values)
if (((Key)v) != Key.None && Keyboard.IsKeyDown((Key)v)
return true;
return false;
}
Good answer here...get the set of all the keystates at once using GetKeyboardState():
Detect if any key is pressed in C# (not A, B, but any)
The above is checking the live state of the keyboard.
If you rely on hooking events up to the KeyDown/KeyUp events to track the state of the keyboard...then this may not be so accurate.
That's becuase you are relying on the message pumping to process and dispatch those KeyDown/KeyUp messages....they may be delivered after the real keyboard state has changed again.
Also because when your bit of code that is interested in the keyboard state is running (usually on the UI thread)...the KeyDown or KeyUp can't interrupt you...as they are dispatched on the UI thread too....that's why using GetKeyBoardState() or the Keyboard.IsKeyDown should be used.
(the above is assuming you want and care about the live state)

Monitoring keyboard activity in C# while my application is in the background

First of all I need to make it clear that I have no interest in keylogging.
I need a way to monitor keyboard activity at the most basic level while my application is in the background.
I don't need to know which keys, I don't need to save any data, I don't need or plan to hide my application at all, all I need is to know when keys are pressed and invoke a method.
I'm looking for the simplest way to do this possible, I know a reasonable amount of C# but nothing too complex as most of my knowledge is self-taught.
I've looked around for some appropriate ways of doing this and I've found nothing useful. All I've found is a bunch of people saying "No, that's illegal" on forums and source code for in depth keyloggers.
If any of you could advise me on a way to achieve this then I would be most appreciative.
You'll need to use Window Hooks:
Low-Level Keyboard Hook in C#
But beware, Windows security, may be protecting us from doing what you want!
You can monitor keyboard and mouse activity in the background with the Nuget package MouseKeyHook (GitHub).
This code detects when a key is pressed:
private IKeyboardMouseEvents _globalHook;
private void Subscribe()
{
if (_globalHook == null)
{
// Note: for the application hook, use the Hook.AppEvents() instead
_globalHook = Hook.GlobalEvents();
_globalHook.KeyPress += GlobalHookKeyPress;
}
}
private static void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
{
Console.WriteLine("KeyPress: \t{0}", e.KeyChar);
}
private void Unsubscribe()
{
if (_globalHook != null)
{
_globalHook.KeyPress -= GlobalHookKeyPress;
_globalHook.Dispose();
}
}
You will need to call Subscribe() to start listening, and Unsubscribe() to stop listening. Obviously you need to modify GlobalHookKeyPress() to do useful work.
I needed this functionality in order to write a utility which will turn on the keyboard backlight on a Lenovo Thinkpad when any key is pressed, including CTRL (which KeyPress doesn't catch). For this purpose, I had to monitor for key down instead. The code is the same except we attach to a different event...
_globalHook.KeyDown += GlobalHookOnKeyDown;
and the event handler signature is different:
private static void GlobalHookOnKeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("KeyDown: \t{0}", e.KeyCode);
}
The library can also detect specific key combinations and sequences. For example:
Hook.GlobalEvents().OnCombination(new Dictionary<Combination, Action>
{
{ Combination.TriggeredBy(Keys.A).Control(), () => { Console.WriteLine("You Pressed CTRL+A"); } },
{ Combination.FromString("Shift+Alt+Enter"), () => { Console.WriteLine("You Pressed FULL SCREEN"); } }
});
Microsoft tells you How to: Handle Keyboard Input at the Form Level. As long as you handle the same event(s) this works for any non web application.
You should also take a look at the other questions here on SO, such as Handling Input from a Keyboard Wedge
You could register Windows Hot Key with RegisterHotKey windows API, look at this blog post :
http://www.liensberger.it/web/blog/?p=207

Categories

Resources