1

I'm new to ReactFX and I'm trying to capture the CTRL and C keys being pressed for a typical copy operation.

How do I capture this effectively into a stream? This is all I could get so far but it is not even compiling...

final EventStream<KeyEvent> keysTyped = EventStreams.eventsOf(myTbl, KeyEvent.KEY_TYPED)
        .reduceSuccessions((a,b) -> new KeyCodeCombination(a.getCode(),b.getCode()), 500);
tmn
  • 11,121
  • 15
  • 56
  • 112

1 Answers1

1

This works for me:

    KeyCombination ctrlC = new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN);
    final EventStream<KeyEvent> keysTyped = EventStreams.eventsOf(text, KeyEvent.KEY_PRESSED)
            // the following line, if uncommented, will limit the frequency
            // of processing ctrl-C to not more than once every 0.5 seconds
            // As a side-effect, processing will be delayed by the same amount
            // .reduceSuccessions((a, b) -> b, Duration.ofMillis(500))
            .filter(ctrlC::match);
    keysTyped.subscribe(event -> System.out.println("Ctrl-C pressed!"));
James_D
  • 201,275
  • 16
  • 291
  • 322
  • 1
    You don't even need `reduceSuccessions`. What `reduceSuccessions` does is that it ignores the first `Ctrl+C` if two of them come within 500ms. In practice this means that you delay `Ctrl+C` handling by 500ms, because `reduceSuccessions` will have to wait 500ms to see whether another `Ctrl+C` arrives within that interval. – Tomas Mikula Apr 23 '15 at 17:58
  • I was assuming the OP wanted to limit the frequency of the operation (maybe transforming large amounts of data from a `TableView` before placing it on the clipboard, for example). But yes, I agree it's hard to see a realistic use case here. – James_D Apr 23 '15 at 18:03
  • Glad to see the ReactFX author here. Yeah, sorry to mislead James. I didn't want to limit the frequency but rather recognize the `CTRL` and `C` keys simultaneously being pressed in close succession to one another. So I'm guessing I just need to remove the `reduceSuccessions()` if that is not necessary for me. – tmn Apr 23 '15 at 18:44
  • 1
    @ThomasN. The `KeyEvent` from JavaFX already contains the information whether `Ctrl` is also pressed, so you don't need to reinvent this logic using ReactFX. – Tomas Mikula Apr 23 '15 at 22:38