Detect delete key on empty input field for mobile devices? - c#

Somehow the OnValueChanged() method doesn't get called when an user presses the delete key on an empty android mobile Inputfield.
I guess as it is empty the value of inputfield is "" before pressing delete and still "" after. Thus there isn't an OnValueChanged() called.
I tried checking for the delete key in Update() but Android doesn't seem to notify an application when the delete key gets pressed.
How can I check if the delete key is pressed?
What I want to achieve is that the Inputfield gets deactivated when the user presses the delete key and the inputfield is empty...
Using c# and Unity

Unfortunately, I am not familiar with C languages, I work with Java and Kotlin. In Java development, the implementation of the solution would look like this:
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
if(keyCode == KeyEvent.KEYCODE_DEL) {
//this is for backspace
}
return false;
}
});
Answer taken from this page: Android EditText delete(backspace) key event
I hope this helps you

As getting an actual KeyEvent from Android for the delete key seems to be quite complicated, I managed to do an easy workaround.
Simply set the text of the Inputfield on activation to " " (Simply space).
That way when the user presses the delete key, the space gets deleted and OnValueChanged gets called.
To avoid problems with the additional space in the input, you can simply implement a method that erases the space when more input is typed.

Related

MessageBox acting weird

I have a very weird problem.
I tried to run this code and whenever I pressed the space key it print multiples 'A' as I want.
comments: the code is inside main, Key.IsPressed is using WinAPI GetKeyState method and the space constant is the virtual key of the space key.
while(true) if(Key.IsPressed(Key.Space)) Console.Write('A');
but when I replace Console.Write('A'); with System.Windows.Forms.MessageBox.Show("A"); it show the message box only on the first time I press the space key (After I close the message box and then press space again).
Even weirder, if I change the key from space to enter it works fine.
The purpose is to create a hidden shortcut manager program so I can't use any key reader that needs focus (like Console.ReadKey).
Any time you use a framework method and it doesn't do what you expect, you should first read the documentation. There you will find information on how the Show method works, for example:
A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

Detect question mark independent from keyboard

The goal is simple: When the user enters chars in a textbox, I want to detect if this char is the question mark key (?).
I don't care what to use (text changed, key down etc...)
One thing to mention: I am working on a german keyboard layout and therefore I need a solution independent from the keyboard (for example: e.Key = Keys.OemQuestion isn't working, it fires when I press the plus (+) key)
Edit: I tried Convert.toString((char)e.Key) which returned \u0095 and e.Key.ToString() which returned OemOpenBrackets
I chose the solution from #HansPassant and managed to do it with the TextInput event.
First in the constructor:
InitializeComponent();
CommandTextBox.AddHandler(TextBox.TextInputEvent, new TextCompositionEventHandler(CommandTextBox_TextInput), true);
You need this code to actually fire the event
in TextInput
if(e.Text == "?")
{
//Do something
}
NOTE:
This does not capture space, control, shift etc.

Convert List of Windows.System.Virtualkey to String

I'm creating an application which will capture a string from an USB attached scanner. I don't want a text box on the form I'm capturing the data on so I've added a handler to the KeyDown event of the window. There is a specific sequence of key presses I can look for to start capturing the data, however, what I can capture is a list of Virtualkeys. That would include LeftShift, v for 'V'
I'm looking for a way to take the array of Virtualkeys and convert that to a string.
Or if you can suggest another way to catpure the text, maybe hidden textbox?
UPDATE
I've positioned a textbox off window and was able to maintain keyboard focus on it so I could capture the data from the barcode scanner.
You can get info whether any VirtualKey is pressed using this:
bool isPressed = Window.Current.CoreWindow.GetKeyState(VirtualKey).HasFlag(CoreVirtualKeyStates.Down);
AFAIK this is the best way of getting to know whether more keys are pressed at the same time.

Outlook 2013 Form Region and the Delete Key

So, I've finally had to deal with this annoying issue. It seems that it's a known "bug" and there doesn't seem to be a great work-around. I was wondering what seems to be the best work around for this.
A little bit of info. In my form region I have a Winform control and a WPF control. The user can't do certain key combinations on the Winform control (Ctrl-A to select all, Delete key deletes email instead of highlighted text), but everything works fine on the WPF control.
I've tried adding the Winform control to the WPF control using a WindowsFormHost, but that made it worse as it wouldn't register the backspace key after that. I tried capturing the delete event for the email, but I can't get the .BeforeDelete to trigger. Same for the Explorer.BeforeItemCut event. Currently I'm trying to capture the WndProc event to re-direct the key events, but it seems like there should be a better/easier way.
Not sure how to continue from here. Any help in direction is welcomed. Below is my how I'm trying to capture email delete event.
Outlook.MailItem _selEmail;
// This does get triggered
private void Explorer_SelectionChange()
{
var actExplorer = this.Application.ActiveExplorer();
if(this.Application.ActiveExplorer().Selection.Count > 0)
{
var selObject = actExplorer.Selection[1];
if(selObject is Outlook.MailItem)
{
_selEmail = selObject as Outlook.MailItem;
_selEmail.BeforeEmailDelete -=
new Outlook.ItemEvents_10_BeforeDeleteEventHandler(Email_BeforeDelete);
_selEmail.BeforeEmailDelete +=
new Outlook.ItemEvents_10_BeforeDeleteEventHandler(Email_BeforeDelete);
}
}
}
// Haven't gotten this to trigger. The Console.Write("") is there
// only for a breakpoint;
private void Email_BeforeDelete(object sender, ref bool cancel)
{
Console.WriteLine("");
}
First of all, I'd suggest breaking the chain of property and method calls and declare each property or method call on a separate line of code. Thus, you will be able to release underlying COM objects inplace. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about that in the Systematically Releasing Objects article in MSDN.
Try to turn off the Single key reading using the space bar option in Outlook which allows you to move quickly through your messages in the Reading Pane. The space bar scrolls the current item. At the end of the item, it jumps to the next unread item in your message list.
Finally, using WPF controls on Outlook forms produce a well-known issue. Outlook has a habit of swallowing various keys and not sending them along to your code or form region. The spacebar, tab, backspace keys are among those affected when the keys are pressed in the reading pane. You can find a similar forum thread.

Is there a way to press or fireup the keys in key board using c# code?

I want the 'Alt' to be pressed by code. It is like firing key-press event with key 'Alt' by the code, not hitting it manually. The need is, I have set 'ShortCut keys for menu, but it (the single underline on key letter) is not visible to the user unless he presses 'alt'. So i need to make the Alt be pressed by default.
Is there a way to 'press' or 'fireup' the keys in key board using c# code?
Check out the System.Windows.Forms.SendKeys class.
You can use the static Send method to send keystrokes to the active window. If you're trying to send keystrokes to another window, you'll need to use the Windows API to activate the other window first.
If you have any control over the operating system on which the program is being deployed, apparently you can force the underlined shortcut letter to always be displayed by going to Control Panel -> Display -> Appearance -> Effects -> Hide underlined letters for keyboard navigation.
(http://www.chinhdo.com/20080902/underlined-letters-windows/)
here is a complete article on use of SendKeys on codeproject
Here is how you simulate input (both mouse and keyboard).
http://msdn.microsoft.com/en-us/library/ms171548.aspx
If you look at the System.Windows.Forms.SendKeys class you will see that it provides you with what you want.
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

Categories

Resources