TweetFollow Us on Twitter

Date FKEY
Volume Number:9
Issue Number:2
Column Tag:Pascal/FKEY

Related Info: Event Manager

A Date-Typing FKEY

A one hour project

By Rob Spencer, East Lyme, Connecticut

This is a one-hour project with two goals: first, to provide a minor but useful tidbit for everyday use, and second, to present a short example of an FKEY that posts and modifies its own events.

For newcomers, an FKEY is a small routine that’s invoked when the user types a command-shift-number sequence, such as the command-shift-3 FKEY that captures the screen to a MacPaint document (under System 6) or Teach Text PICT document (System 7). An FKEY consists of stand-alone code with no global variables and usually little or no user interface; see Roy Lovejoy’s recent article (“FKEYs in THINK Pascal, Easy...”, MacTutor, June 1992, p. 53-55) for more discussion.

This FKEY does something simple and useful for those of us who type letters and memos every day: when the user types command-shift-6 (or whatever number from 0 to 9 you choose), the FKEY types the current date into the current document or edit field. It does this by getting the date from the Toolbox and sending it to the event queue as a sequence of keyDown events. The text appears in your document exactly as if you had typed it.

ASSEMBLE THE EVENT

The heart of the FKEY is the SendAString routine that posts each character of its input string as a keyDown event for the current application to catch. All that’s necessary is to assemble the appropriate message (a longint) for each character.

The low byte of message is charCode, which is just ord(theStr[i]). The next byte is the keyCode, which specifies which key was pressed. For most uses keyCode can be zero, since most keyDown dispatch routines ignore the keyCode and look only at charCode, like this:

with myEvent do begin
 myChar := CHR(BitAnd(message, charCodeMask));
 if BitAnd(modifiers, cmdKey) <> 0 then...

However, my testing with keyCode = 0 showed that some applications didn’t receive the proper string, so to be safe I had to do a little more work to fill in the correct keyCode byte.

Key codes are defined in the System’s KCHR resource, but rather than retrieve and parse that, we use a small bit of it in a fixed look-up table string (called keyMap in the listing). This contains the correct keyCode values for all letters, numbers, and the comma. The space character is handled separately, rather than make keyMap too long. Finally, keyCode is shifted to the second byte and added to charCode to produce the correct message.

REMOVE THOSE MODIFIERS

In the first version of the FKEY I simply used PostEvent and got unexpected results: since the user must have the command and shift keys down to invoke the FKEY, the keyDown events that it posts come with the cmdKey and shiftKey modifier bits set. Thus if the month is September, the first keyDown that the application sees will be command-S, and it will promptly save the current document! What we want is the FKEY to send the keyDowns without any modifiers; the solution is to use PPostEvent and then clear the appropriate bits from the new event, as shown in the listing.

TESTING and DEBUGGING

As both Roy Lovejoy and the THINK Pascal demo FKEY “BlockComment” point out, debugging an FKEY is simple. Just build a tiny program that includes the FKEY unit and then call the obligatory FKEY entry point called main. In my program I then use THINK’s Text window to receive the keyDown events, so we can see what day it is.

USING THE FKEY

Use ResEdit to paste the FKEY resource into the word processing application of your choice, or, if you want the FKEY universally available, paste it into the System file. That’s it!

BUT WAIT, THERE’S MORE!

Though FKEY’s are as old as the Mac, they aren’t widely appreciated. Perhaps this is partly because of what I just wrote above: “Use ResEdit...”. I decided to provide an easier way, though for the sake of brevity I’ll only mention it here: on the code disk for this month is a HyperCard stack which does a one-button installation of the FKEY to your System file.

Figure 1: bonus installation stack

This stack does this job with an original XFCN and XCMD. The XFCN, SystemResources, returns a list of System resources of a given type (to see if the FKEY already exists), while the XCMD, InstallFKEY, does the actual installation. The stack also includes the THINK Pascal source code for these externals.

LISTINGS
unit DateFKEY;
{ Types today’s date when the user types }
{ cmd-shift-6. In THINK Pascal 4.0 by    }  
{ Rob Spencer, August 1992.              }

interface

 procedure main;

implementation

 procedure main;

 { ----------------------------- }

 function SendAString (theStr: str255): OSErr;

 const
 { keyMap is a subset of the }
 { KCHR System resource.     }
 keyMap = 
 'ASDFHGZXCV*BQWERYT123465=97*80*OU*IP*LJ*K**,*NM';
 space = char(32);

 var
 i, keyCode: integer;
 theChar: char;
 theErr: OSErr;
 message, modifiers, modifierMask: longint;
 myQPtr: EvQElPtr;

 begin
 theErr := noErr;
 modifierMask := BitNot(shiftKey + cmdKey);

 if theStr <> '' then
 begin
 FlushEvents(keyDown, 0);
 for i := 1 to length(theStr) do
 begin
 { Get the proper keyCode. }
 theChar := theStr[i];
 if theChar in ['a'..'z'] then
 { Make theChar uppercase for }
 { our look-up string.        }
 theChar := char(ord(theChar) - 32);
 if theChar = space then
 keyCode := $31
 else
 keyCode := pos(theChar, keyMap) - 1;
 if keyCode = -1 then
 keyCode := 0;

 { Assemble the message.   }
 message := BitShift(keyCode, 8) +
 ord(theStr[i]);

 { Post the keyDown event. }
 theErr := PPostEvent(keyDown, message,
 myQPtr);
 if theErr <> noErr then
 leave;

 { Now strip off the cmdKey }
 { and shiftKey modifiers.  }
 modifiers := BitAnd(myQPtr^.evtQModifiers, 
 modifierMask);
 myQPtr^.evtQModifiers := modifiers;
 end;
 end;
 SendAString := theErr;
 end;

 { ============== main ============== }

 var
 dateStr: str255;
 tempLong: longint;

 begin
 { The queue can only hold 20 characters, }
 { so we strip off the day of the week.   }
 GetDateTime(tempLong);
 IUDateString(tempLong, LongDate, dateStr);
 if dateStr <> '' then
 if pos(char(32), dateStr) > 0 then
 Delete(dateStr, 1, pos(char(32), dateStr));
 if SendAString(dateStr) <> noErr then
 SysBeep(10);
 end;

end.

{ ====== shell program for debugging ===== }

program DateFKEYtest;

 uses
 DateFKEY;

 var
 myStr: str255;

begin
 main;  { Call our FKEY }
 { Make an active input window    }
 { to receive the keyDown events. }
 ShowText;
 read(myStr);
 writeLn;
 writeLn(myStr);
end.

{ =========== end of listings =========== }

Figure 2: the project window for debugging

Figure 3: the project window for the stand-alone FKEY

Figure 4: set up for FKEY #6.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.