#!/usr/bin/perl -w # # Copyright 2000 by Michael Coyle # Released under GPL. # # Call it with: # [an error occurred while processing this directive] # # Get the file name from the browser... $file_name = $ENV{'QUERY_STRING'}; # Open the file... open (EP, $file_name); # Print to the browser... print "Content-Type: text/html \n\n"; # Load the file and keep spitting it out to the browser... while () { chomp; print "$_ "; } # Close the file and go home... close EP #!/usr/bin/perl -w # # Copyright 2000 by Michael Coyle # Released under GPL. # # Call it with: # [an error occurred while processing this directive] # # Get the file name from the browser... $file_name = $ENV{'QUERY_STRING'}; # Open the file... open (EP, $file_name); # Print to the browser... print "Content-Type: text/html \n\n"; # Load the file and keep spitting it out to the browser... while () { chomp; print "$_ "; } # Close the file and go home... close EP

3D
3D Photo Gallery (Part 1)
3D Photo Gallery (Part 2)

Audio
Poor Man's MIDI
Make A Metronome
iPod Tricks (Part 1)
iPod Tricks (Part 2)
iPod Tricks (Part 3)
Laugh Track Machine
Audio Player with Reverb
Shepard Melody
RB Phone Home
Build a Drum Machine

Custom Controls and Windows
Double Click Listbox
Draggable Metal Window
Double Click Canvas
Custom Buttons
Custom Buttons Part II
iTunes-style Listboxes
Custom Controls


General RB
Scrolling Windows
Using Mesage Dialogs
Case-Sensitive Word Finder
Introduction to Stacks
Wiggle Window
JPEG in PDF
Listbox Checkboxes
Background Applications
Listbox Auto-Find
Virtual Volumes
Time Tracker
Software Distribution (Part 1)
Software Distribution (Part 2)
Software Distribution (Part 3)
Software Distribution (Part 4)
Exceptions
Tips and Tricks
Text Clippings Made Easy

Graphics
Drawing a Simple Gradient
The SpriteSurface: Space Game
Image Spinner
Cropping Graphics (Part 1)
Cropping Graphics (Part 2)
Cropping Graphics (Part 3)
Cropping Graphics (Part 4)
Shimmer Graphics
Lissajous Figures
Simple Screen Capture
Vector Graphics
Kaleidoscope Images
Stegonography
Spirals!
Image Table
RB Magnifying Lens
Screen Capture
Color Picker Tutorial

Hacks
Ghost Grab
Speedy Mouse Extension
iTunes Plugins
iTunes Skinner

Mac OS X
Global Hot Key Event (Carbon Events)
Login Welcomer (Carbon Events)
Add/Remove Buttons
Resizable Sheets
Mac OS X Preferences Window
Using Sheets in REALbasic
Build a Bundle (Part 1)
Build a Bundle (Part 2)
Dock Your Passwords
Mac OS X Debugging
REALbasic Mac OS X Icon Tutorial
Animate Your Dock
RB and the Command Line

Menus
Window Menu
Templates Menu
Listbox Menu

Novelty
Guessing Game
Calendar Trivia
Tile Mixer
Zip Code Finder
Happy Valentine's Day
Merlin Simulator (Part 1)
Merlin Simulator (Part 2)
Merlin Simulator (Part 3)
Buzzword Machine
AppleSoft BASIC

Printing
Print to PDF

Registration
Registration Code Validation
Network Registration Codes

Resources
Picture Extractor (Part 1)
Picture Extractor (Part 2)

Serial
Caller ID (Part 1)
Caller ID (Part 2)
Caller ID (Part 3)

Speech
Speech Recognition

Socket Communication
Easy Peer-to-Peer File Sharing
MacPAD Version Checking
Display Web Image In Canvas
HTML IMG Tags
Version Tracking
Even Smarter Instant Messaging
Web Tiler
JavaScript and REALbasic
Stock Ticker (Part I)
Stock Ticker (Part 2)
AIM Mate

XML Manipulation
Simple XML Introduction

Video
Big Brother Video Capture

Note: All articles without a byline were written by Erick Tejkowski. When cleaning the site I removed them because the code differed from page to page, and I have yet to put them back in.

resexc2.gif (20k)

   




REALbasic for Dummies
by Erick Tejkowski

$19.99 @ Amazon





Files are in Stuffit 6.5 or earlier, or ZIP format.
Download Stuffit Expander

Tell us about a bad link.

Global Hot Keys by Seth Willits
05-15-04




The Login Welcomer Tutorial
To introduce you to using AddressOf and the Carbon Event system, we created a very simple example which says "Welcome" and "Goodbye" when you switch between users. Reading through that example to familiarize yourself with how Carbon Events work will make this tutorial easier to understand.

The Global Hot Keys Tutorial
In this tutorial, we'll explore how to register for a hot key event. A hot key is a combination of keys that when pressed notifies our application. The application can be in the foreground or background which makes this and ideal solution for bringing up transparent UIElement background applications, like Freaky Trash Manager.

Registering for the Hot Key
In addition to using the InstallEventHandler function (which we use to be notified of hot key events), we'll be using one specifically for hot keys RegisterEventHotKey, which notifies the system that we want to know when the hot key combination is pressed. When it is, our event handler installed with InstallEventHandler is fired. The parameters to the RegisterEventHotKey function are similar to that of InstallEventHandler. They are:

  • UInt32 inHotKeyCode
  • UInt32 inHotKeyModifiers
  • EventHotKeyID inHotKeyID
  • EventTargetRef inTarget
  • OptionBits inOptions
  • EventHotKeyRef * outRef

The inHotKeyCode parameter is simply the key code of key you want (this code varies between keyboard layouts, I'm just going to use the QWERTY layout) and inHotKeyModifiers is a combination (Bitwise.BitOr) of the modifier constants which are defined in the image below.





The first part of the method we'll use to install the handler involves the Declares. The three Declares we need are InstallEventHandler, GetApplicationEventTarget, and NewEventHandlerUPP. They are as follows:


Sub InstallHotKey()
   Declare Function RegisterEventHotKey Lib Carbon (KeyCode as Integer, KeyModifiers as Integer, EventHotKeySig as Integer, EventHotKeyID as Integer, EventTargetRef as Ptr, options as integer, ByRef HotKeyRef as integer) as Integer
   Declare Function InstallEventHandler Lib Carbon (eventTarget as Ptr, handler as Ptr, numTypes as integer, types as ptr, userdata as integer, ByRef EventHandlerRef as integer) as Integer
   Declare Function GetApplicationEventTarget Lib Carbon () as Ptr
   Declare Function NewEventHandlerUPP Lib Carbon (address as Ptr) as Ptr
   
   
   dim err, eventRef as integer
   dim eventType, keyID as MemoryBlock
   dim attributes as integer
   dim modifiers as integer
   
   // Event Types List
   eventType = NewMemoryBlock(8)
   eventType.StringValue(0, 4) = kEventClassKeyboard
   eventType.Long(4) = kEventHotKeyPressed



So far we've only done the standard Declare statements, dimmed our variables, and created the EventTypeSpec for the hot key event. Below we create an ID for our hot key registration and then install the event handler. This ID is the direct object parameter sent to our event handler. It is how we know which hot key was pressed.


   // Key ID
   keyID = NewMemoryBlock(8)
   keyID.StringValue(0, 4) = "t3st"
   KeyID.Long(4) = 0
   
   // Install Event Handler
   err = InstallEventhandler(GetApplicationEventTarget(), NewEventHandlerUPP(AddressOf HotKeyPressed), 1, eventType, 0, HotKeyEventHandlerRef)
   if err <> noErr then
      MsgBox "InstallEventHandler" + EndOfLine + EndOfLine + str(err)
   end if



Below we use Bitwise Or to combine the modifiers we want to be part of the key combination. Here we're using Command, Option, and Control. The key itself is the "T" key (&h11).


   // Register HotKey
   modifiers = Bitwise.BitOr(modifiers, cmdKey)
   modifiers = Bitwise.BitOr(modifiers, optionKey)
   modifiers = Bitwise.BitOr(modifiers, controlKey)
   err = RegisterEventHotKey(&h11, modifiers, keyID.Long(0), keyID.Long(4), GetApplicationEventTarget(), 0, HotKeyRef)
   if err <> noErr then
      MsgBox "RegisterEventHotKey Error" + EndOfLine + EndOfLine + str(err)
   end if
End Sub


The event handler itself is far simpler:


Function HotKeyPressed(EventHandlerCallRef as Integer, EventRef as Integer, UserData as Integer) As Integer
   Window1.Show
   
   return noErr
End Function



Now our handler is so simple since we're only registering for one key. If we registered for multiple keys, we'd need to get the direct object parameter from the EventRef to find out which key was pressed.

To unregister a hot key, we use the UnregisterEventHotKey function which takes a pointer to the hot key reference. Note that we'd also need to unregister the event handler too if for some reason we did unregister the hot key.



Sub UninstallHotKey()
   Declare Function UnregisterEventHotKey Lib Carbon (EventHotKeyRef as Integer) as Integer
   Declare Function RemoveEventHandler Lib Carbon (EventHandlerRef as Integer) as Integer
   
   
   dim err as integer
   
   err = UnregisterEventHotKey(HotKeyRef)
   if err <> noErr then
      MsgBox "UnregisterEventHotKey Error" + EndOfLine + EndOfLine + str(err)
   end if
   
   err = RemoveEventHandler(HotKeyEventHandlerRef)
   if err <> noErr then
      MsgBox "RemoveEventHandler Error" + EndOfLine + EndOfLine + str(err)
   end if
End Sub



Normally you wouldn't need to unregister the hot key or uninstall the event handler since you'd want to be notified the entire time your application is running, and when your application quits, the unregistering is basically handled for you thanks to Mac OS X.

Finished
Well other than the code here, I've added a bit to the project just to make it tad more interesting. So go ahead and grab the project below, run it, and press Cmd-Opt-Cntrl-T.

As always, you can download the project for this tutorial.






Please support ResExcellence by Visiting our Sponsors. One click makes a difference.


Download REALbasic and create your own software!

#!/usr/bin/perl -w # # Copyright 2000 by Michael Coyle # Released under GPL. # # Call it with: # [an error occurred while processing this directive] # # Get the file name from the browser... $file_name = $ENV{'QUERY_STRING'}; # Open the file... open (EP, $file_name); # Print to the browser... print "Content-Type: text/html \n\n"; # Load the file and keep spitting it out to the browser... while () { chomp; print "$_ "; } # Close the file and go home... close EP #!/usr/bin/perl -w # # Copyright 2000 by Michael Coyle # Released under GPL. # # Call it with: # [an error occurred while processing this directive] # # Get the file name from the browser... $file_name = $ENV{'QUERY_STRING'}; # Open the file... open (EP, $file_name); # Print to the browser... print "Content-Type: text/html \n\n"; # Load the file and keep spitting it out to the browser... while () { chomp; print "$_ "; } # Close the file and go home... close EP