Mouse input

Text grid and graphics windows have the nifty ability to recognize mouse clicks and return the coordinates of the selected character or pixel. To take advantage of this ability, first you need to alert the program to be on the lookout for mouse clicks in the window or windows where you want them. Let's say that you have a graphics window, gg_compasswin, in which you place an image of an ornate eight-spoke compass rose. You want the player to be able to click on any spoke and have it converted into a command to go in the appropriate direction. First, you need to drop the following line into Initialise():


The game will be on the lookout for one, and only one, mouse click in the window with the compass in it. If none occurs, that's fine; the program will continue to accept regular line input as usual. But if one does occur, HandleGlkEvent() will be triggered.

HandleGlkEvent() has cropped up in previous sections (see Graphics in a graphics window, for instance, or Sound and music) but this is an especially clear instance of how the routine works. As always, HandleGlkEvent() takes the arguments "ev" and "context"; this time, ev, an array, gets quite a workout. When the program detects a mouse click, it fills up the array with the following information:

Your HandleGlkEvent routine, then, will look (at least in part) something like this:

   [ HandleGlkEvent ev context;

      switch (ev-->0) {



            ! Here we would put code to analyze the results and

            ! generate new instructions. For instance, we might say

            ! that if ev-->2 is between 50 and 100, and ev-->3 is

            ! between 0 and 50, then that must be the northern spoke

            ! of the compass, so we'll issue a command to go north.



Note that after successfully recognizing and reacting to one mouse click, the game will stop looking out for mouse input unless we tell it to watch for another one -- hence the second glk_request_mouse_event() call.

Now, because the game is waiting for line input, you can't just issue a command like <<Go n_obj>>;. Instead, do the following. First, go back up to the line where you declare HandleGlkEvent() and declare three new local variables: abortres, newcmd, and cmdlen. Then, return to where you were and cancel the line input like so:

   glk_cancel_line_event(gg_mainwin, 0);

Then, come up with a command that'll do the same thing. In this example, "go north" would do nicely. Set that as the new command like this:

   newcmd = "go north";

Next, copy the following code verbatim:

   cmdlen = PrintAnyToArray(abortres+WORDSIZE,


   abortres-->0 = cmdlen;

What this does is complete the process of telling the computer that in this particular instance, clicking in that particular area of the graphics window is exactly equivalent to typing in "go north" and pressing Enter. Now, you can either place that very phrase at the prompt to let the player know that's what's been done, or you can do something like this:


   print "(mouse click)";



Which is the better method depends on personal preference and the particular case at hand. In any event, the last line in one of these code blocks should be "return 2;", to indicate that this turn is over. And voila -- you've dealt with mouse input.

Next section: Hyperlinks
Or return to the table of contents