Hello
You may try this. Tested under 10.6.8.
Good luck,
H
(*
For test, press some key during the delay created by [1].
Key accompanied with command modifier is ignored as specified by |:mask| option in [2].
Note that the key pressed will be typed in current document as well.
To exit, cancel the dialogue.
*)
repeat
delay 0.2 -- [1]
set c to key_pressed({|:mask|:"k"}) -- [2]
if c ≠ "" then
display dialog c & " (" & c's id & ") is pressed"
else
display dialog "none"
end if
end repeat
on key_pressed(options) -- v0.2
(*
record options : control options
full spec = {|:mask|:m, |:sleep|:s}
defaults = {|:mask|:"", |:sleep|:0.1}
string m : modifier flags; e.g. 'ck' = control + command
a = capslock
s = shift
c = control
o = option
k = command
* key pressed accompanied with any modifiers specified in m is ignored
integer i : sleep [sec] put as guard interval before return
*)
set defaults to {|:mask|:"", |:sleep|:0.1}
set {|:mask|:m, |:sleep|:s} to options & defaults
if m = "" then set m to "%"
set s to "" & s
set ruby to "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby"
do shell script ruby & " <<'EOF' - " & m's quoted form & " " & s's quoted form & "
require 'osx/cocoa'
include OSX
m, s = ARGV[0..1]
s = s.to_f
mtable = {
'a' => KCGEventFlagMaskAlphaShift,
's' => KCGEventFlagMaskShift,
'c' => KCGEventFlagMaskControl,
'o' => KCGEventFlagMaskAlternate,
'k' => KCGEventFlagMaskCommand,
}
mf = m.split(//).inject(0) { |mf, x| (m = mtable[x]) ? mf | m : mf }
sst = KCGEventSourceStateCombinedSessionState
cc = []
unless CGEventSourceFlagsState(sst) & mf != 0 # ignore key accompanied with any modifiers in mf
(0x00..0x7f).each do |vk|
if CGEventSourceKeyState(sst, vk)
ev = CGEventCreateKeyboardEvent(nil, vk, true) # vk down
nsev = NSEvent.eventWithCGEvent(ev) # 10.5 or later
begin
cc << nsev.characters.to_s
rescue
end
end
end
end
puts cc.join('')
sleep s if s > 0.0 # optional guard interval
EOF"
end key_pressed