#!/usr/bin/python # Created by Matteo Rattotti on 13/03/06. # Copyright (c) 2006 - 2007 Shiny Frog. All rights reserved. # Contact matteo.rattotti@shinyfrog.net for any problem # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. from Foundation import * from AppKit import * from Carbon.CarbonEvt import RegisterEventHotKey, GetApplicationEventTarget from Carbon.Events import cmdKey, controlKey from PyObjCTools import NibClassBuilder class hotkeyView(NibClassBuilder.AutoBaseClass): def initWithFrame_(self, frameRect): super(hotkeyView, self).initWithFrame_(frameRect) hotk = NSUserDefaults.standardUserDefaults().objectForKey_("deliHotkey") keycode = NSUserDefaults.standardUserDefaults().integerForKey_("deliKeyCode") if hotk == None or hotk == "None" or hotk == "invalid key": self.hotKeyRef = None else: NSLog("registering "+ hotk +" hotkey") self.hotKeyRef = RegisterEventHotKey(keycode, cmdKey | controlKey, (0, 0), GetApplicationEventTarget(), 0) if hotk == None: self.hotkey = NSString.stringWithString_("None") else: self.hotkey = hotk # Maybe init also this? self.keycode = None self.acceptHotKey = 0 return self def drawRect_(self, rect): borderWidth = 2.0 fontsize = 12 bgColor = None if self.acceptHotKey == 1: bgColor = NSColor.colorWithDeviceRed_green_blue_alpha_(0.62, 1.0, 0.70, 1.0) else: #bgColor = NSColor.colorWithCalibratedWhite_alpha_(0.90, 1.0) bgColor = NSColor.colorWithCatalogName_colorName_("System", "selectedControlColor") fontsize = 18 boxRect = self.bounds() bgRect = boxRect bgRect = NSInsetRect(boxRect, borderWidth/2.0, borderWidth/2.0) bgRect = NSIntegralRect(bgRect) #bgRect.origin.x += 0.5 #bgRect.origin.x += 0.5 minX = NSMinX(bgRect) midX = NSMidX(bgRect) maxX = NSMaxX(bgRect) minY = NSMinY(bgRect) midY = NSMidY(bgRect) maxY = NSMaxY(bgRect) radius = 25.0 bgPath = NSBezierPath.bezierPath() bgPath.moveToPoint_(NSMakePoint(midX, minY)) bgPath.appendBezierPathWithArcFromPoint_toPoint_radius_(NSMakePoint(maxX, minY), NSMakePoint(maxX, midY), radius) bgPath.appendBezierPathWithArcFromPoint_toPoint_radius_(NSMakePoint(maxX, maxY), NSMakePoint(midX, maxY), radius) bgPath.appendBezierPathWithArcFromPoint_toPoint_radius_(NSMakePoint(minX, maxY), NSMakePoint(minX, midY), radius) bgPath.appendBezierPathWithArcFromPoint_toPoint_radius_(bgRect.origin, NSMakePoint(midX, minY), radius) bgPath.closePath() bgColor.set() bgPath.fill() bColor = NSColor.grayColor() bColor.set() bgPath.setLineWidth_(borderWidth) bgPath.stroke() NSColor.blackColor().set() d = NSMutableDictionary.dictionary() d.setObject_forKey_(NSFont.fontWithName_size_("Monaco", fontsize), NSFontAttributeName) strSize = self.hotkey.sizeWithAttributes_(d) center = NSMakePoint(NSMidX(rect) - (strSize.width/2.0), NSMidY(rect) - (strSize.height/2.0)) self.hotkey.drawAtPoint_withAttributes_(center, d) def unsetHotkey(self): #oldKeyCode = NSUserDefaults.standardUserDefaults().integerForKey_("deliKeyCode") #self.hotkey = UnregisterEventHotKey(oldKeyCode) if self.hotKeyRef != None: self.hotKeyRef.UnregisterEventHotKey() self.hotKeyRef = None def saveHotkey(self): self.unsetHotkey() # Saving into prefs hotkey and it's keycode NSUserDefaults.standardUserDefaults().setObject_forKey_(self.hotkey, "deliHotkey") NSUserDefaults.standardUserDefaults().setInteger_forKey_(self.keycode, "deliKeyCode") if self.hotkey != "None" and self.hotkey != "invalid key": # Registering new keycode self.hotKeyRef = RegisterEventHotKey(self.keycode, cmdKey | controlKey, (0, 0), GetApplicationEventTarget(), 0) # Keyboard handling functions def keyDown_(self, theEvent): if self.acceptHotKey == 1: self.hotkey = theEvent.characters() self.keycode = theEvent.keyCode() v = ord(self.hotkey) hotkey = theEvent.characters() c = hotkey.characterAtIndex_(0) if c == NSDeleteFunctionKey or c == 0x7F: self.hotkey = NSString.stringWithString_("None") self.acceptHotKey = 0 self.saveHotkey() elif v < 97 or v > 122: self.hotkey = NSString.stringWithString_("invalid key") else: self.acceptHotKey = 0 self.saveHotkey() self.setNeedsDisplay_(1) # Our view will accept keyboard event def acceptsFirstResponder(self): return 1 # Mouse handling functions def mouseDown_(self, theEvent): self.hotkey = NSString.stringWithString_("Press a key \n [a-z]") self.acceptHotKey = 1 self.setNeedsDisplay_(1) # Our view will accept mouse click event def acceptsFirstMouse_(self, theEvent): return 1 # Flipping cords of nsview def isFlipped(self): return 1