#include <Events.h>
#include <SegLoad.h>

#define TIME_BETWEEN_WNE	-1

void CheckEvents(void){
	static 	char key;
	static  short		part;
	static	RgnHandle	cursorRgn;
	static	Boolean		gotEvent,quit=0;
	static	EventRecord	event;
	static	Point		mouse;
	static	short 		item;
	static int loop;
	static	WindowPtr	whichWin;
	static 	long time_for_next_WNE = 1;
	static	union {
				Point p;
				long lw;
			} f;

    if ((LMGetTicks()>time_for_next_WNE) ){
		loop = 0;
		gotEvent = WaitNextEvent(mDownMask|highLevelEventMask|keyDownMask, &event, 0, nil);
		if (gotEvent) switch (event.what){
				case mouseDown:
						part = FindWindow(event.where, &whichWin);
						switch ( part ) {
							case inMenuBar:   // process a mouse menu command (if any) 
								MenuSelect(event.where);
								break;
						}
						break;
				case kHighLevelEvent:
					f.p = event.where;
					if (f.lw == kAEQuitApplication){
						quit = true;
						break;
					}
					break;
				case keyDown:
					if (event.modifiers & cmdKey){
						//Deal with Cmd keys in message window
						key = toupper(event.message & charCodeMask);
						switch (key){
							case 'Q':{ quit=true; break;}
						}
						break;
					}
					break;
		}
		if (quit) ExitToShell();   
		time_for_next_WNE = LMGetTicks() + TIME_BETWEEN_WNE;
	} else loop++;
}
							
