# TextEditor::keyPressed() doesn't capture key press events

**URL:** <https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702>\
**Category:** General JUCE discussion\
**Created:** [April 7, 2017, 7:31am UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702 "2017-04-07T07:31:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [April 7, 2017, 7:31am UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/1 "2017-04-07T07:31:47Z")

</div>

This issue only occurs on macOS. It is strange that it can capture few key press events, eg. ‘delete’, ‘tab’, ‘return’, ‘4 arrows’, ‘cmd + any char-key’…all others can’t, however enabled IME or not.

Tested JUCE-demo (on macOS), also the same.

JUCE 4.3.1 (git master)  
macOS:10.12.3  
Xcode: 8.2.1

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 7, 2017, 8:28am UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/2 "2017-04-07T08:28:09Z")

</div>

Sorry, but can you give an example of a keypress which _doesn’t_ work?

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [April 7, 2017, 8:51am UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/3 "2017-04-07T08:51:53Z")

</div>

？？

JUCE Demo may be the best one? You could try it, Jules. Run Demo, give a breakpoint to TextEditor::keyPressed(), then make some input (feel free hit some keys) on Misc’s textEditor, you’ll find out what I’m saying is really true.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 7, 2017, 10:14am UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/4 "2017-04-07T10:14:33Z")

</div>

But we use text editors everywhere, and that method obviously must be receiving keypresses, otherwise text editors would never work at all!

Maybe I just don’t understand the point you’re trying to make?

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [April 7, 2017, 10:34am UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/5 "2017-04-07T10:34:51Z")

</div>

Yes, Jules. That’s why I’m so confused.

We can input any characters, but seems lot of them never ‘through out’ keyPressed(). it means: if I want capture some input, I can’t implement the goal.

DId you try it in Demo?

Let me say a little more. Currently, I’m using the TextEditor::keypressed() to implement this function:

just like any IDE’s auto-complete, Intelligence tips, etc… when user input some certain characters, it’ll show a popup menu, the menu items are the tips (auto-complete content). What I did this is capture every key event by keyPressed(), then do some judge… it’s a little bit complex beyond my English level…

In addition of this, I also use keyPressed() to impelment ‘punctuation auto-match’. eg. when user input ‘[’, the ‘]’ will appear immediately, if something has been selected, the matches punctuation will auto wrap it.

and other funcy functions, shortcuts, etc… I made the TextEditor very powerful you couldn’t imagine:) All these things severe dependenced on TextEditor::keyPressed().

All of these work perfectly on Windows… but, on mac, nothing and nothing, because that method never had been called except a few key, eg.‘return’, ‘delete’, ‘tab’…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 7, 2017, 10:45am UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/6 "2017-04-07T10:45:37Z")

</div>

OK, I see the confusion - some of the events get sent via IME, so bypass the keyPress callback. If you want to see what’s going on, try putting your breakpoint in TextEditor::insertTextAtCaret() instead, which will always get called.

It might be better to intercept the events via a TextEditor::Listener rather than by overriding methods?

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [April 7, 2017, 11:02am UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/7 "2017-04-07T11:02:23Z")

</div>

Thank God you saw my confusion 🙂

All of those you said I have been used. One main reason I used keyPressed() is they have other things to do… if I put the code in, the logic will make me mad perhaps… Currently, my Editor.cpp is nearly 2000 lines. it’s a sub class of TextEditor.

and it’s nothing about IME! When I unabled (switch off) IME on mac, it’ll the same result. On Windows, both IME or not are perfect.

One more confused: why the same lib and code on Windows can, but on mac can’t?

However, I’ll give it a try again in accordance with your instructions…Thanks!

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [April 9, 2017, 1:42pm UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/8 "2017-04-09T13:42:04Z")

</div>

Finally, I refactoried these function 🙂 Guess what, Jules? I overrided TextEditor::insertTextAtCaret(), cooperate MultiTimer class. let insertTextAtCaret() method do more work 🙂 I know you’ll criticize on this, but ,it works perfect both on mac and Windows :))

Thanks!

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [April 9, 2017, 1:46pm UTC](https://forum.juce.com/t/texteditor-keypressed-doesnt-capture-key-press-events/21702/9 "2017-04-09T13:46:29Z")

</div>

and, perhaps you didn’t notice, insertTextAtCaret() will be called twice when enabled IME and input a char. The first time it is an empty and will cancel the highlight text (include press ‘del’, ‘backspace’ etc.)… so I have to use MultiTimer class and do more judge in keyPressed()…

However, I did it 🙂
