File tree Expand file tree Collapse file tree
rxjava-contrib/rxjava-swing/src/main/java/rx Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1515 */
1616package rx ;
1717
18+ import static rx .Observable .filter ;
19+
1820import java .awt .event .ActionEvent ;
1921import java .awt .event .KeyEvent ;
22+ import java .util .Set ;
2023
2124import javax .swing .AbstractButton ;
2225import javax .swing .JComponent ;
2326
2427import rx .swing .sources .AbstractButtonSource ;
2528import rx .swing .sources .KeyEventSource ;
29+ import rx .util .functions .Func1 ;
2630
2731/**
2832 * Allows creating observables from various sources specific to Swing.
@@ -50,4 +54,20 @@ public static Observable<ActionEvent> fromButtonAction(AbstractButton button) {
5054 public static Observable <KeyEvent > fromKeyEvents (JComponent component ) {
5155 return KeyEventSource .fromKeyEventsOf (component );
5256 }
57+
58+ /**
59+ * Creates an observable corresponding to raw key events, restricted a set of given key codes.
60+ *
61+ * @param component
62+ * The component to register the observable for.
63+ * @return Observable of key events.
64+ */
65+ public static Observable <KeyEvent > fromKeyEvents (JComponent component , final Set <Integer > keyCodes ) {
66+ return filter (fromKeyEvents (component ), new Func1 <KeyEvent , Boolean >() {
67+ @ Override
68+ public Boolean call (KeyEvent event ) {
69+ return keyCodes .contains (event .getKeyCode ());
70+ }
71+ });
72+ }
5373}
You can’t perform that action at this time.
0 commit comments