Listen for keyboard events (iOS)

500 views
Skip to first unread message

Anatoly Pulyaevskiy

unread,
Nov 30, 2017, 9:46:22 PM11/30/17
to Flutter Dev

Hi everyone,


In my project I need to capture user input from keyboard in a form of KeyPress events.

TextInputConnection doesn't seem to provide this level of granularity.

I did find RawKeyboard which seems to be what I need, but it doesn't support iOS at this point.


Wondering if this is because of some limitations on the platform side or just needs someone to get this done?

Also does it have to be implemented in the engine or could be done as a plugin?


--

Anatoly

Eric Seidel

unread,
Nov 30, 2017, 10:57:31 PM11/30/17
to Anatoly Pulyaevskiy, Flutter Dev
Can you explain more about what effect you're trying to achieve?

Or give an example of what iOS API you would use were you writing your application in Obj-C or Swift?

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Barth

unread,
Nov 30, 2017, 11:13:24 PM11/30/17
to Eric Seidel, Anatoly Pulyaevskiy, Flutter Dev
As far as I know, iOS doesn't expose KeyPress-level events.

Anatoly Pulyaevskiy

unread,
Dec 1, 2017, 12:07:16 AM12/1/17
to Flutter Dev
Sure. In short I need a text editor which:

a. Is a rich-text editor and can also embed media (images)
b. Allows for collaborative editing and/or synchronization across multiple devices.

The (a) is not really relevant to my original question though.
For (b) I was going to implement Operational Transformation system. This requires capturing user input as they type and then applying changes to the document, potentially merging with changes arrived from other users/devices via server.

So this is still text-related problem (not a game or something else keyboard-controlled).
I'd really appreciate any feedback I can get.

For now, I'm about to try and build a plugin (similar to FlutterTextInputPlugin found in the engine) which additionally:

- exposes a stream of text changes from the keyboard.
- can accept text changes coming from Dart side (updates from other devices/users).

Though it seems a bit unfortunate to maintain native text input in the background with a copy of the document.

Kris Giesing

unread,
Dec 1, 2017, 12:11:35 AM12/1/17
to Anatoly Pulyaevskiy, Flutter Dev
Do you want to capture raw key events, or do you want to allow the input system to pre-process them and give you the results?

If it's the latter, then perhaps the existing Flutter text input support could be made flexible enough to do what you want. Can you elaborate on what granularity of control you need that is currently missing?

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

Anatoly Pulyaevskiy

unread,
Dec 1, 2017, 1:06:06 AM12/1/17
to Flutter Dev
I shouldn't need raw key events as I'm only interested in text input which means:

- inserting text at given position
- deleting text at given position
- pasting / replacing text at given position

Currently TextInputClient only provides updateEditingValue() with 
TextEditingValue which contains full text of an input field.
For my use case I'd need to track individual inserts and deletes so something
like this should be sufficient:

class TextEditingValue {
  /// Current value of a text input.
  String text;
  
  /// Selection (or cursor position) after [change].
  TextSelection selection;
  
  /// NEW! Information about the change which triggered this update.
  TextEditingChange change;
}

/// Only keyboard events are needed.
enum ChangeType { insert, delete }

class TextEditingChange {
  /// Type of change.
  ChangeType type;
  /// If type is [ChangeType.insert] contains actual inserted text, `null` 
  /// otherwise.
  String insertedText;
  /// There should be no need to have anything extra here.
  /// For deletes we already know cursor position on the Dart side so it
  /// should be possible to detect if a delete removed current selection or
  /// just one character at current position.
}

I _think_ this should be good enough for my use case. Hope this makes sense now.

Anatoly Pulyaevskiy

unread,
Dec 1, 2017, 12:19:07 PM12/1/17
to Flutter Dev
To be honest, at this point it's a bit hard to come up with exact API which would solve my use case, as I'm just starting on this project.

There is a workaround I can use in the meantime: for every TextEditingValue received from the platform side compose a diff with local value and convert it into a set of individual delete and insert operations.

It is far from ideal but it should unblock my work so that I can build a prototype. This should give me a better idea about what API change is needed. I still think it would most likely be along the lines of what I sent yesterday: adding TextEditingChange field to TextEditingValue, but I'd have more confidence in it.

Adam, Kris, eseidel

Once I have better idea about the API I can submit a PR to the engine and we can continue this discussion there, if that's OK with you?

Kris Giesing

unread,
Dec 1, 2017, 3:34:25 PM12/1/17
to Anatoly Pulyaevskiy, Flutter Dev
That all seems reasonable to me. I suspect (having done a very cursory investigation) that improving the API in the way you suggest would require changes to both the engine and the framework, because I think it requires changes to the information passed over the system channel between native and Dart layers.

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages