MonoTouch: How can I monitor the keyboard and determine KeyPress events? - xamarin.ios

How can I monitor the keyboard and determine KeyPress events ?

If you want to monitor key press events for UITextView, then override the UITextViewDelegate's ShouldChangeText() method.
If you want to monitor key press events for UITextField, there's a similar method in UITextFieldDelegate called ShouldChangeCharacters().
Find more info on the usage of these methods here and here.

The ShouldChangeCharacters of UITextField is called BEFORE the text is changed and it is not the same as the Changed event of UITextView. This means that getting the text on the ShouldChangeCharacters or ShouldChangeText method, will allways return the text BEFORE the last key was pressed.
The UITextView has a Changed event like the KeyPress event. UITextField has not such event yet.
A workaround for the UITextField can be found here:
bugzilla.xamarin.com/show_bug.cgi?id=864
NSNotificationCenter.DefaultCenter.AddObserver
(UITextField.TextFieldTextDidChangeNotification, (notification) =>
{
Console.WriteLine ("Character received! {0}", notification.Object ==
TextField);
});

Related

Dismissing the keyboard upon clicking on the UIDatePicker

I can't seem to dismiss the keyboard when the user click on a UITextField then clicks directly starts interacting with the UIDatePicker control.
I am using the following to dismiss the keyboard upon a tap:
// The following code will remove the keyboard when it is not editing...
var g = new UITapGestureRecognizer(() => View.EndEditing(true));
View.AddGestureRecognizer(g);
Can you please tell me how I can dismiss the keyboard when the user clicks on the UIDatePicker control to start adjusting the date?
You have to call .ResignFirstResponder() on the ui element. That should dismiss the keyboard.
xamarin recipe example for keyboard dismissing
xamarin documentation explaining ResignFirstResponder
Try this code:
var tap = new UITapGestureRecognizer ();
tap.AddTarget (() => this.View.EndEditing (true));
this.View.AddGestureRecognizer (tap);
tap.CancelsTouchesInView = false;

How to catch global keyPress events in Ember

I'm sure I'm missing something obvious, but: How do I respond to a keypress event anywhere on the page?
For example, say I had a video player and I wanted to pause it if a user pressed the spacebar at any point. Or, I'm writing a game and want the arrow keys to direct a character, without regard for specific views or subviews.
Defining App.ApplicationView = Ember.View.create({ keyPress: whatever }) doesn't seem to work. I would have thought that all keypress events would bubble up to that.
I guess what I want to do is:
$(document).keypress(function(e) {
alert('You pressed a key!');
});
Except through Ember.
If you are looking for a global keypress your code example above is probably the best solution. The ember application is attached to the body (or a root element you've defined) and would require some sort of focus on the view. For example you could do:
App.ApplicationView = Em.View.extend({
keyUp: function(){
alert(1);
}
});
This will work, but the application view or a child will need to have focus. This is useful, for example, if you wanted to capture a key event of all input fields.

MonoTouch.Dialog: Dismissing keyboard by touching anywhere in DialogViewController

NOTE: There are two similar SO questions (1) (2), but neither of them provides an answer.
TL;DR: How can one dismiss the keyboard in a MonoTouch.Dialog by letting the user touch any empty space in the view?
I'm writing an app using MonoTouch.Dialog and a UITabBarController. One of my tabs is "Settings"...
When the user starts typing, the keyboard obstructs the tabbar...
Using MonoTouch.Dialog, the only way to dismiss the keyboard is to go to the last field and press the "return" key. Considering the fact that the user cannot press any tab until the keyboard is gone, I would like a better way to do it. Namely, to dismiss if the user taps anywhere else on the screen.
Without MonoTouch.Dialog, it's a snap: simply override TouchesBegan and call EndEditing. But this doesn't work with MT.D. I've tried subclassing DialogViewController and overriding TouchesBegan there, but it doesn't work. I'm currently at a loss.
Or, I wonder, would I be better off ditching the tabbar so I can use a UINavigationController with a "Back" button on top, which won't be hidden by the keyboard?
I suggest you use a tap gesture recognizer that will not cause interference with the TableView event handlers:
var tap = new UITapGestureRecognizer ();
tap.AddTarget (() => dvc.View.EndEditing (true));
dvc.View.AddGestureRecognizer (tap);
tap.CancelsTouchesInView = false;
You missed my question about it also: Can the keyboard be dismissed by touching outside of the cell in MonoTouch.Dialog?
:-)
This is my #1 feature request for MonoTouch.Dialog.
To answer your question: No. It is not possible. I have searched and asked around and have not found any answers.
I assume because it is just a sectioned (grouped) table and if it wasn't sectioned, there wouldn't be any spot to click. However, that is just my speculation.
I wish that miguel or someone that works on monotouch would answer this and say if it is even possible. Possibly a future enhancement?
I figured out a workaround that satisfies me well enough, so I'm answering my own question.
// I already had this code to set up the dialog view controller.
var bc = new BindingContext (this, settings, "Settings");
var dvc = new DialogViewController (bc.Root, false);
// **** ADD THIS ****
dvc.TableView.DraggingStarted += (sender, e) => {
dvc.View.EndEditing (true);
};
This will dismiss the keyboard whenever the user drags the view a little bit. There's no touch event I could find associated with the tableview, so this is the next best thing. I'd welcome any other ideas. Cheers!
One workaround to use the dragging gesture instead of the tap as proposed (that do not interfere with the table view gestures) is to override MonoTouch.Dialog.DialogViewController.SizingSource (or MonoTouch.Dialog.DialogViewController.Source if you don't want uneven rows) and give it to the DialogViewController. I don't know if it is very clean or safe.
public class CustomTableViewSource : MonoTouch.Dialog.DialogViewController.SizingSource
{
public CustomTableViewSource(MonoTouch.Dialog.DialogViewController dvc) : base(dvc)
{
}
public override void DraggingStarted(UIScrollView scrollView)
{
base.DraggingStarted(scrollView);
if (scrollView != null)
{
scrollView.EndEditing(true);
}
}
}

how to check which key is pressed in LUWIT?

I am using LWUIT package for my j2me application.In my application, i have extends Component class and then draw strings on the component.Now i want to get the key code and then draw string on the component based on the key pressed.how do i know which key is pressed in LWUIT? I want to capture key press event on LWUIT and draw strings on screen.Is this possible in LWUIT?
Is there any way to draw strings on screen without using Component in LWUIT?
You can add a label to a container which is normally how we do things in LWUIT and use the component based UI (see our demos).
You can override keyReleased and do your event handling there, but your component needs to be focusable to receive key events. Alternatively you can bind a key listener to the form or override the forms key callback methods.

How do I disable Qt's behavior on Linux of capturing arrow keys for widget focus navigation

I'm working on a Qt app that is developed primarily on MacOS, but that is then built and tested on Linux as well.
We define a keyPressEvent() method in our main window class to respond to certain keyboard events. Among others, we respond to Qt::Key_Left, Qt::Key_Right and Qt::Key_Space. This works great on MacOS. On Linux, however, we never get these events.
Doing some Googling (and confirming it with our app's behavior on Linux), it seems that the reason for this is that Qt uses these keys for keyboard navigation of button widgets in the application's GUI. If I press the arrow keys on Linux, I cycle through all the active button widgets, selecting each on in turn. If I click the spacebar, the currently selected button is pressed.
All I've been able to find so far by Googling is suggestions as to how to either subclass or apply filters to specific buttons to avoid this behavior by having the button ignore the event and pass it along. But I don'w want to have to do this for every button widget I ever put into my GUI. That's just lame.
Is there a way to disable this behavior globally and allow my app code to actually get ALL of the arrow key and spacebar events?
You can set add global event listener to catch these events.
In the window constructor:
QApplication::instance()->installEventFilter(this);
In the window's eventFilter method:
bool MainWindow::eventFilter(QObject *object, QEvent *event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent* key_event = static_cast<QKeyEvent*>(event);
qDebug() << "key" << key_event->key() << "object" << object;
//...
}
return false;
}
If you have processed an event, you should return true from this function. You can use object variable to find out the source of event. For example, it can be some QPushButton. You can check if this button is a child of your main window if it's necessary (if you have multiple top windows).
Another way is to disable focus for buttons completely. Set buttons' focusPolicy to NoFocus. Then they will not catch key events.
Also you can subclass QPushButton and reimplement keyPressEvent with empty implementation.

Resources