Forum Archive

Use Keycommand in conjunction with superview and pyui.

MOD0

Hi, i wrote code in order to use my program with Bluetooth keyboard.(some part quoted from here)

I succeeded in some print code(pleases press the D key in your bluetooth-keyboard).
But when I press A (i expected label show me Pusshed-A)
only called that message

AttributeError (Line-35)
Alabel = sender.superview['Alabel']
AttributeError: 'int' object has no attribute 'superview'

How do i use Keycommand with superview?

code is here(Please make FC.pyui)
(FC.pyui include label. label name is Alabel)

```
from objc_util import *
import ui

UIKeyCommand = ObjCClass('UIKeyCommand')
modifiers = {(1<<17): 'Shift', (1<<18): 'Ctrl', (1<<19): 'Alt', (1<<20): 'Cmd', (1<<21): 'NumPad'}

def keyCommands(self, _cmd):
cmd_key_flag = (1<<20)
key_a = UIKeyCommand.keyCommandWithInput_modifierFlags_action
('A', 0,'keyCommandAction:')
key_s = UIKeyCommand.keyCommandWithInput_modifierFlags_action_('S', 0, 'keyCommandAction:')
key_d = UIKeyCommand.keyCommandWithInput_modifierFlags_action_('D', 0, 'keyCommandAction:')

commands = ns([key_a, key_s, key_d])
return commands.ptr

def canBecomeFirstResponder(_self, _cmd):
return True

def keyCommandAction_(_self, _cmd, sender):
self = ObjCInstance(_self)
key_cmd = ObjCInstance(sender)
flags = key_cmd.modifierFlags()
modifier_str = ' + '.join(modifiers[m] for m in modifiers.keys() if (m & flags))
key_input = key_cmd.input()
print 'Input: "%s" Modifiers: %s' % (key_input, modifier_str)
if str(key_input) == 'A':
A(sender)
elif str(key_input) == 'S':
S(sender)
else:
D(sender)

def A(sender):
Alabel = sender.superview['Alabel']
Alabel.text = str('Pusshed-A')
def S(sender):
print('pusshed-S')

def D(sender):
print('pusshed-D')

FCCF = create_objc_class('FCCF', UIView, [keyCommands, canBecomeFirstResponder, keyCommandAction_])
main_view = ui.load_view('FC')
v = FCCF.alloc().initWithFrame_(((0, 0), (1, 1)))
v.becomeFirstResponder()
ObjCInstance(main_view).addSubview_(v)
main_view.present('sheet')

JonB

You seem to be confusing sender used in normal ui actions, with the input argument to the keycommand action, which is a UIKeyCommand object.

if you are just trying to access a specific view, access it from the global or from an instance attribute in a custom class... iirc uikeycommands don't tell you which control caught the keypress.

MOD0

JonB
I find that I misunderstood Keycommand, so i try to add subview.
Tkank you for reply my asking.