And on with the show....

Over the past couple of months, I have covered the wimp_poll, looking at reason codes 0 to 9.

The last time, I asked if you could alter (by examining part1b.c and part2a.c) to intercept the mouse key presses. Below is the solution and an extension on handling the keyboard.

Adding in the mouse

If you recall the structure the poll routine passes back when the mouse is clicked, you will recall within there the variable int buttons; this contains the result of the mouse click. It is this which needs to be added to the poll function like this

case 6 : mouse_click (wb->poll.mouse_click.buttons);
         break;

this has told the program that from now on, when the mouse is clicked inside of my application, that the function mouse_click has to be passed with the value contained from within wb->poll.mouse_click.buttons.

The function for dealing with a simple mouse click (by that, I mean a single click, it does not require handling drags or any other values other than 1,2 or 4) and outputting which button was pressed is not a difficult affair.

All we need to do is determine which button has been pressed and put that into the a string which makes sense. The following is an example of how this may be done

void mouse_click (int button)
{
 char butn[10],total_message[100];
 switch (button)
 {
  case 1 : strcpy(butn,"adjust");
           break;
  case 2 : strcpy(butn,"menu");
           break;
  case 4 : strcpy(butn,"select");
           break;
 }
 sprintf(total_mess,"You have clicked on the %s button",butn);
 rep_error(255,total_mess);
}

As you can see, it is only a simple example, but it does the job.

The key handler (poll reason code 8) is handled in a similar way, remembering though that we have to handle the F12 key by simply throwing it away.

Again, in the event_poll function, we need to add

case 8 : key_press(wb->poll.key_pressed.code);
         break;

with an associated function for the key press being

void key_press(int key)
{
 char mess[100];
 switch (key)
 {
  case 0x1cc : _swix(Wimp_ProcessKey,_IN(0),key);
               break;
  default    : sprintf
                (mess,"You have pressed key (ASCII) %d",key);
               rep_error(255,mess);
               break;
 }
}

What is this call to Wimp_ProcessKey though?

If an application responds to reason code 8, it must make this call if it receives a key which is either not recognised or is unwanted by the application. The swi takes they key code. This has to be performed to allow other applications to act upon it.

While we are altering the event_poll routine, we can alter the switch routine to allow us to examine the mouse entering and leaving the window. Again, four lines added will do the job.

case 4 : printf ("\a");
         break;
case 5 : printf ("\a");
         break;

This will produce a beep (\a is a control code in C to make a beep) when the mouse enters or leaves the window. However, this will only ever happen once. The reason for this is in the window creation definition function. We have the workarea_flags set to 3<<12. This means that the action is only acted on once. If this is changed to 2<<12, the mouse will always cause a beep.

If you examine the listing supplied with this tutorial (part 3a.c), you can see how these changes have been implemented.

Next time, error handling and the use of MessageTrans.