Reputation: 42420
Given the following code:
[:input {:type "text"
:value (:text @app-state)
:on-change (fn [e]
(if (= 31 (.-keyCode e))
(println "ENTER")
(println "NOT ENTER")))}]
How to change the if
condition so that enter keypresses can be distinguished from normal keys? All properties in e
except target
seem to be null.
Upvotes: 15
Views: 5171
Reputation: 5217
With key
.
[:input
{:on-key-press
(fn [e]
(if (= (.-key e) "Enter")
(.log js/console "Enter")
(.log js/console "Not Enter")))}]
Also of interest is :on-key-up
and :on-key-down
.
Upvotes: 10
Reputation: 17859
that's how to fix it:
:on-key-press
(rather than :on-change
),
because "enter" doesn't trigger :on-change
event (it obviously doesn't change the text)use charCode
instead of keyCode
(not an expert in js, but keyCode
doesn't work for me in firefox)
[:input {:type "text"
:value (:text @app-state)
:on-key-press (fn [e]
(println "key press" (.-charCode e))
(if (= 13 (.-charCode e))
(println "ENTER")
(println "NOT ENTER")))}]
Upvotes: 22