Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: kas1e on April 30, 2004, 05:13:13 PM

Title: 'keypressed' handlers programming
Post by: kas1e on April 30, 2004, 05:13:13 PM
hello all. first, agayn sorry for poor enlgish and easy questions. so.
i want write full system-compatible apps on sasc wich 'keypressed' handlers.
for example, i want trap 'alt+1' combination. application must be in
background as commoditie. i mean:

#include <;;;;;;>
main()
{
    if 'alt+n' printf to con: "you press alt+n\n"
    else printt to con: "you press other key\n"
}


what library i must using for trap need keys? what functions? etc. easy/little
example will be very good.

thnx all.
Title: Re: 'keypressed' handlers programming
Post by: drHirudo on April 30, 2004, 07:31:32 PM
Do you want to program key handler for the program when it's window is active, or at anytime?
For the first case, you just need to add IDCMP port for RAWKEY or VANILLAKEY when you open the window, for example

my_window = (struct Window *) OpenWindowTags(NULL, WA_Left,0,WA_Top,0,WA_Width,180,WA_Height,160,WA_IDCMP,RAWKEY,WA_NewLookMenus,TRUE,WA_Flags,SMART_REFRESH|ACTIVATE,WA_Title, "drHirudo",WA_WBenchWindow,TRUE,TAG_END,NULL);

Later in you program you need to take the events from the userport of the window - there are 2 ways - passive (prefered) and active (takes more cpu resources). Here is the passive way:

Wait ( 1 << my_window->UserPort->mp_SigBit );

If an event comes to your program, you have to handle it, but first you need to know what's the event.

  while (my_message = (struct IntuiMessage *) GetMsg (my_window->UserPort))
   { class=my_message->Class;
     code=my_message->Code;
    ReplyMsg(my_message);
   }
 /* We got it. Actually we have the last event, and ignore the previous. */

/* For a keypress: */

 if (class == RAWKEY) {
  switch (code) {
 /* Do stuff according the pressed key/keys. */
  case 1: printf("The key 1 was pressed.");break;
  case 2: printf("The key 2 was pressed.");break;
  default: printf("Some other key was pressed.");break
 }
}

That's it - a simple key handler.

For this stuff you simply need the Intuition library.

IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",0 );
 if ( !IntuitionBase )
  {printf("Something terrible had happened!");return(-1);}

At the end don't forget to close the window and the library:

CloseWindow ( my_window );

CloseLibrary ( IntuitionBase );

 :-D

Regards
Title: Re: 'keypressed' handlers programming
Post by: Kronos on April 30, 2004, 08:48:42 PM
Don't think that the given example will be any use since it is a commodity that is planned .....

http://us.aminet.net/aminet/util/cdity/mouseblanker.lzh (http://us.aminet.net/aminet/util/cdity/mouseblanker.lzh)

Includes source, haven't looked at it, but you would probraly need to add some sort of hotkey-filtering to it.

Might dig out one of my old sources if needed.
Title: Re: 'keypressed' handlers programming
Post by: Thomas on May 01, 2004, 12:28:10 PM

If you want to create a commodity you should read the section about commodities in the RKRM. Will be quite complicated without.

If the program shall run on OS1.3 as well, you cannot use commodities, you then have to add an input handler to input.device. Again, read the section about input handlers in the RKRM or it will become a little bit complicated.

Bye,
Thomas