Detecting Keypress?


#1

I have a textEditor component, and when a user presses Enter Key I want to detect this, how can I do this? What listeners do I need to add to what, how do I set my callback function, some example would be appriciated… Cheers


#2

I think you might possibly be looking for TextEditorListener::textEditorReturnKeyPressed!


#3

have a look at juce_Label.cpp since Labels are TextEditorListeners


#4

Thanks jules, but how can I set up the call back for key presses, I’m doing the following but not working

[code]class myComp : public Component,
public KeyListener

{
private:

TextEditor* textBox;

public:
myComp()

{
	
textBox = new TextEditor(T("Console"),0);//set to 0x25cf for password blobs
textBox->setColour (TextEditor::backgroundColourId, Colours::lightgrey);
textBox->setColour (TextEditor::outlineColourId, Colours::grey);
  textBox->setMultiLine (true, false);
  textBox->setReturnKeyStartsNewLine (true);
addAndMakeVisible(textBox);

}

~myComp(){}

void paint (Graphics& g)
{
g.fillAll (Colours::darkgrey);
}

bool keyPressed (const KeyPress& key)
{
if(key == KeyPress::returnKey) show(T(“Return Key Was Pressed!!”));

  return true;

}

};[/code]


#5

Well you could start by using a TextEditorListener, and not a KeyListener…


#6

Well isn’t there a way to detect key presses for the whole component which could have many textEditors, not just for the text Editor? I have already wasted many hours trying the work out the exact way to do some thing sooo simple. Also, instead of saying what I could be doing you could easly give an example, you don’t have to be condescending!

If juce had proper documentation with examples that would be a great help, unfortunately that does not exist, so new developers have to work most things out by luck, some things that may seem trivial to you may take hours for beginners. How do you detect keypresses on any component? Say I have a form app with many TextEditors and when the user hits return I want this form to process. Do I need to listen for keypress on all of these textEditors(using TextEditorListener), or can I listen for keys for the Component they live in so if user hits enter, process form? Cheers…


#7

I would say that your enclosing component should be a TextEditorLisener and you should register this as a listener for each of the (child) TextEditor form fields. Your enclosing component would then respond to textEditorReturnKeyPressed() to process the form.

Seriously, look at juce_Label.cpp as an example. And in general look in Juce itself for examples of how you can do various things.


#8

[quote=“splitn101”]Well isn’t there a way to detect key presses for the whole component which could have many textEditors, not just for the text Editor? I have already wasted many hours trying the work out the exact way to do some thing sooo simple. Also, instead of saying what I could be doing you could easly give an example, you don’t have to be condescending!

If juce had proper documentation with examples that would be a great help, unfortunately that does not exist, so new developers have to work most things out by luck, some things that may seem trivial to you may take hours for beginners. How do you detect keypresses on any component? Say I have a form app with many TextEditors and when the user hits return I want this form to process. Do I need to listen for keypress on all of these textEditors(using TextEditorListener), or can I listen for keys for the Component they live in so if user hits enter, process form? Cheers…[/quote]

Oooh, you’ve really got my goat here sunshine.

JUCE comes with just about the most comprehensive documentation you’ll ever find with any library. I would advise you to take at least one look at it before you shoot your mouth off at jules for even bothering to give you such a basic tip!

Look at the docs for TextEditor. It says perfectly clearly in the description ‘see also TextEditorListener’; if you click that, you’ll be taken to a page showing all the callbacks you can make use of, as well as ‘see also TextEditor::addListener’.

You derive a class from TextEditorListener (e.g. your containing component) and register it with your child TextEditor using e.g. editor->addListener(this).

Seriously, have a look at the docs before you start overreacting like this again!


#9

Thanks Haydxn. Yes, I don’t really see what else I could have put in the docs that would make this any clearer. If there’s something that’s obviously missing, then please let me know, but I can’t write detailed descriptions of how to do every possible thing that every user might possibly want to do!


#10

Relax buddy, If you read I said documentation with examples!. Yes, the docs are good(very good) but there are hardly any examples. If people can’t ask questions to your satisfaction in these forums, and if jules was really bothered by giving me that tip, he can say so him self. Juce is a great library, I just think it need s more examples. Anyways, thanks for the help.


#11

well, there is the Juce demo, which has examples of just about everything the library has to offer.