Sign up

How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

Hi Mike,

ComboBoxText's signal, onChanged, responds to both key events and mouse selection events, but I'd like write a callback that responds only to the mouse.

What I have so far [see reportSelected() below] uses writeln(getActiveText()) to illustrate harnessing this signal and it works as I'd like when mouse-selecting from the list. But typing into the Entry results in:

M
Mo
Mon
Mond... etc.

being echoed to the terminal and it looks messy, so I'm thinking there must be a way to filter out key presses so that no matter what user action triggers the signal, I can weed out the events I don't want to respond to. But to do that, I need access to the Event and I can't figure out how to do that from within a callback that isn't passed an Event object. After a few hours of searching and experimenting, I'm out of ideas. Can you advise, please?

Here's the class I'm working on:

class DayComboBoxText : ComboBoxText
{
	string[] days = ["yesterday", "today", "tomorrow"];
	bool entryOn = true;
	
	this()
	{
		super(entryOn);
		
		foreach(day; days)
		{
			appendText(day);
		}

		addOnKeyRelease(&echoToTerminal);
		addOnChanged(&reportSelected);

	} // this()


	bool echoToTerminal(Event event, Widget w)
	{
		bool stopHereFlag = true;
		
		if(event.type == EventType.KEY_RELEASE)
		{
			GdkEventKey* keyEvent = event.key;
			
			if(keyEvent.keyval == GdkKeysyms.GDK_Return)
			{
				writeln(getActiveText());			
			}
		}

		return(stopHereFlag);
		
	} // echoToTerminal()

	
	void reportSelected(ComboBoxText cbt)
	{
		writeln(getActiveText());			
		
	} // reportSelected()
	
} // class DayComboBoxText

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

On 09-05-2019 18:07, Ron Tarrant wrote:

Hi Mike,

ComboBoxText's signal, onChanged, responds to both key events and mouse selection events, but I'd like write a callback that responds only to the mouse.

What I have so far [see reportSelected() below] uses writeln(getActiveText()) to illustrate harnessing this signal and it works as I'd like when mouse-selecting from the list. But typing into the Entry results in:

M
Mo
Mon
Mond... etc.

being echoed to the terminal and it looks messy, so I'm thinking there must be a way to filter out key presses so that no matter what user action triggers the signal, I can weed out the events I don't want to respond to. But to do that, I need access to the Event and I can't figure out how to do that from within a callback that isn't passed an Event object. After a few hours of searching and experimenting, I'm out of ideas. Can you advise, please?

Here's the class I'm working on:

I cant sem to find a way to do that.

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

On Thu, 9 May 2019 22:27:16 +0200, Mike Wey wrote:

I cant sem to find a way to do that.

Bummer. Thanks for trying, though.

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

On Thu, 9 May 2019 22:27:16 +0200, Mike Wey wrote:

[stuff deleted] a way to do that...

I came across this tidbit in which Main is called recursively and an extra signal is attached until Main quits. I tried a few ways to implement this, but couldn't get it to work.

So I have a couple of questions:

1) Is this possible in GtkD?
2) Any thoughts on implementation?

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

On Fri, 10 May 2019 12:58:00 GMT, Ron Tarrant wrote:

On Thu, 9 May 2019 22:27:16 +0200, Mike Wey wrote:

[stuff deleted] a way to do that...

I came across this tidbit in which Main is called recursively and an extra signal is attached until Main quits. I tried a few ways to implement this, but couldn't get it to work.

So I have a couple of questions:

1) Is this possible in GtkD?
2) Any thoughts on implementation?

I forgot to include the link: http://www.getoffmalawn.com/blog/blocking-gtk-signal-handlers-without-freezing-your-application

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

I also came across some talk of creating signals/events from scratch, but Signal.d and Event.d don't seem to have functions to do that.

I'm assuming this would be possible using functions.d and (what?) wrapping it? Totally lost on this one, so any ideas/suggestions will be appreciated.

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

On 10-05-2019 14:58, Ron Tarrant wrote:

On Fri, 10 May 2019 12:58:00 GMT, Ron Tarrant wrote:

On Thu, 9 May 2019 22:27:16 +0200, Mike Wey wrote:

[stuff deleted] a way to do that...

I came across this tidbit in which Main is called recursively and an extra signal is attached until Main quits. I tried a few ways to implement this, but couldn't get it to work.

So I have a couple of questions:

1) Is this possible in GtkD?
2) Any thoughts on implementation?

I forgot to include the link: http://www.getoffmalawn.com/blog/blocking-gtk-signal-handlers-without-freezing-your-application

That should be possible.

The get_something_async function in D would be something like this:

string get_something_async()
{
     gulong id;

     void activate(Entry entry)
     {
	Signals.handlerDisconnect(entry, id);
         Main.quit()
     }

     id = entry.addOnActivate(&activate);

     Main.run();

     return entry.getText();
}

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

On 10-05-2019 16:20, Ron Tarrant wrote:

I also came across some talk of creating signals/events from scratch, but Signal.d and Event.d don't seem to have functions to do that.

I'm assuming this would be possible using functions.d and (what?) wrapping it? Totally lost on this one, so any ideas/suggestions will be appreciated.

I don't know the talk, but if i`m correct you would use gsignalnew to
register a new signal, and call gsignalemit to well emit the signal.

A user could then use gsignalconnect the connect to the signal.

In GtkD there is Signals.newv and Signals.emitv witch are the array
based versions of gsignalnew and gsignalemit.

And Signals.connect to connect a function, a delegate, or something with
an opCall to the Signal.

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

On Fri, 10 May 2019 19:49:22 +0200, Mike Wey wrote:

On 10-05-2019 16:20, Ron Tarrant wrote:

I don't know the talk, but if i`m correct you would use gsignalnew to
register a new signal, and call gsignalemit to well emit the signal.

A user could then use gsignalconnect the connect to the signal.

In GtkD there is Signals.newv and Signals.emitv witch are the array
based versions of gsignalnew and gsignalemit.

And Signals.connect to connect a function, a delegate, or something with
an opCall to the Signal.

On Fri, 10 May 2019 19:33:41 +0200, Mike Wey wrote:

The get_something_async function in D would be something like this:

string get_something_async()
{
     gulong id;

     void activate(Entry entry)
     {
	Signals.handlerDisconnect(entry, id);
         Main.quit()
     }

     id = entry.addOnActivate(&activate);

     Main.run();

     return entry.getText();
}

Thanks for the info, Mike. I'll let you know how I make out.

Re: How to Separate Keypresses from Mouse Actions in an addOnChanged() Callback?

I found a solution and it was far simpler than I thought it would be. In the onChanged callback, if the index of the active text is -1, it hasn't been added to the list yet. So to distinguish between typing and selecting from the drop-down list, this does the trick:

	void onChanged(ComboBoxText cbt)
	{
		if(getIndex(getActiveText()) !is -1)
		{
			writeln("this is a list item: ", getActiveText());
		}
		else
		{
			writeln("and this isn't: ", getActiveText());
		}
		
	} // onChanged()

I just wasn't working with all available data.