0096: Hardware III – Keyboard and Pointer

For times when we need to grab key press or mouse pointer events for special circumstances, we can do that through the Display > Seat hierarchy as well. We’ll start with…

The Keyboard

Results of this example:
Current example output
Current example output
Current example terminal output
Current example terminal output (click for enlarged view)

The usual suspects show up in the preamble, but if we’re going to react to keystrokes, we need to add a Keymap:

Seat seat;
Display myDisplay;
Device _keyboard;
Keymap keymap;

We won’t be fooling around with changing the Keymap at this point, but we do need access to the default Keymap so we can put names to faces, so to speak. To set that up, we look to the constructor:

this(string title)
{
	super(title);

	myDisplay = Display.getDefault();
	seat = myDisplay.getDefaultSeat();
	_keyboard = seat.getKeyboard();
	keymap = Keymap.getDefault();

	addOnDestroy(&quitApp);
	addOnKeyPress(&onKeyPress);
		
} // this()

The first four lines after the call to the super() constructor do the drill-down to the default Keymap. Then come the signal hook-ups, the second being the one of interest this time around because it hooks up…

The onKeyPress() Callback

bool onKeyPress(GdkEventKey* eventKey, Widget widget)
{
	string pressedKey;
	int keys;
		
	pressedKey = keymap.keyvalName(eventKey.keyval);
	writeln("The keyval is: ", eventKey.keyval, " which means the ", pressedKey, " was pressed.");

	return(true);
		
} // onKeyPress()

The Keymap.keyvalName() function returns the name of a pressed key as a string which means we don’t have to dig any further before feeding the result into a switch statement or a series of if/else’s.

Just keep in mind that any callback function you hook up to the onKeyPress signal needs a Boolean return value.

Now let’s move on to…

The Mouse Pointer

Results of this example:
Current example output
Current example output
Current example terminal output
Current example terminal output (click for enlarged view)

Again, we have the drill-down from Display to Seat to pointing Device, but unlike the Keyboard, we need a couple more objects to help us make the most of getting pointer data…

In the Preamble

We add a Screen as well as x/y coordinates:

Seat seat;
Display myDisplay;
Device pointer;
Screen screen;
int x, y;

And after doing the same type of drill-down in the constructor and hooking up signals:

this(string title)
{
	super(title);

	myDisplay = Display.getDefault();
	seat = myDisplay.getDefaultSeat();
	pointer = seat.getPointer();
		
	addOnDestroy(&quitApp);
	addOnEnterNotify(&onEnterNotify);
		
} // this()

We have…

A Callback

Which looks like this:

bool onEnterNotify(Event event, Widget widget)
{
	Window lastEventWindow, windowAtPosition;

	pointer.getPosition(screen, x, y);
	lastEventWindow = pointer.getLastEventWindow();
	windowAtPosition = pointer.getWindowAtPosition(x, y);

	writeln("position: x = ", x, ", y = ", y); // position is in display coordinates
	writeln("lastEventWindow: ", lastEventWindow);
	writeln("windowAtPosition: ", windowAtPosition);
	
	return(true);
		
} // onEnterNotify()

I picked the onEnterNotify signal because it only reports once when you move the pointer over the window. Anything else will fill the terminal with hundreds of lines of output in a demo like this, so I’ll leave it to you to explore those other signals. If you need a refresher, just about anything under the topic Mouse should help you orient yourself.

Conclusion

And that pretty much wraps up our look at hardware for now. In case you haven’t looked at a calendar lately, this also wraps up 2019. We’ll start the new year with a discourse on the HeaderBar, a replacement for a Window.Titlebar, which turns out to be a rather interesting beast.

So, happy New Year, happy coding and see you next week.

Comments? Questions? Observations?

Did we miss a tidbit of information that would make this post even more informative? Let's talk about it in the comments.

You can also subscribe via RSS so you won't miss anything. Thank you very much for dropping by.

© Copyright 2023 Ron Tarrant