Identifier for 'Enter' with onKeyPress event for v8.1.5

I am trying to change some parameters using the onKeyPress event with a text field using the enter key as the trigger to make the updates. This worked fine in v8.0.xx. After updating to 8.1.5, it appears that when enter is pressed it returns an empty string or at least that is what it looks like. Below are the if statements that I have attempted to return True for but have not been able to.

if event.key == 'Enter' or event.Key == 'enter':
if event.key == '':
if event.key == ' ':

Example outputs from the output console when simply printing event.key every time a key is pressed:

15:05:25.706 [Browser Thread: 64239] INFO Perspective.Designer.Workspace - a
15:05:26.390 [Browser Thread: 64239] INFO Perspective.Designer.Workspace - f
15:05:26.790 [Browser Thread: 64239] INFO Perspective.Designer.Workspace - d
15:05:27.178 [Browser Thread: 64239] INFO Perspective.Designer.Workspace - s
15:05:28.300 [Browser Thread: 64239] INFO Perspective.Designer.Workspace -

The last line is what is returned when Enter is pressed

I think the event.key is the newline character \n. so you do something like this:

if event.key == '\n':
     #Code for Enter pressed

I think it should do what you want.

After a little bit of playing around, it seems that the event.key for the “Enter” key is a newline character in the onKeyPressed event, but if I use the onKeyDown event, the event.key is “Enter”. This is on 8.1.1, however.

2 Likes

It’s \n, indeed. I’d recommend using repr() to enhance your prints in these scenarios, e.g.:
system.perspective.print(repr(event))

15:08:18.390 [Browser Thread: 54470] INFO Perspective.Designer.Workspace - {u'type': 'keypress', u'eventPhase': 3L, u'bubbles': True, u'cancelable': True, u'timeStamp': 26735.02999963239, u'defaultPrevented': False, u'isTrusted': True, u'detail': 0L, u'key': '\n', u'location': 0L, u'ctrlKey': False, u'shiftKey': False, u'altKey': False, u'metaKey': False, u'repeat': False, u'charCode': 13L, u'keyCode': 0L, u'which': 13L}
2 Likes

Thanks for the info. That looks like a handy function!