Key event processing

13 views
Skip to first unread message

Simon

unread,
Apr 29, 2019, 7:44:27 PM4/29/19
to Cappuccino & Objective-J
Hi all,

So I have a view (SphereView) in which I have OpenGL rendering taking place, and if I create the containing window for the view using:

var mask = CPBorderlessBridgeWindowMask;
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:mask];
var contentView = [theWindow contentView];
[contentView setBackgroundColor:[CPColor colorWithHexString:@"EEEEEE"]];

// Create GL View
_view = [[SphereView alloc] initWithFrame:[contentView bounds]];
[_view setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[_view setCenter:[contentView center]];
[contentView addSubview:_view];


... then all goes well. I can zoom in, out and around using the W, A, S, D keys. If, however, I change the window mask to be 

var mask CPClosableWindowMask;
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(50,50,1024,768) 

... then my key processing disappears. Sphereview.j has 

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (void)keyDown:(CPEvent)theEvent
{
    switch ([theEvent keyCode]) {
        case 119: _keyZ =  0.1; break; // w
        case 115: _keyZ = -0.1; break; // s
        case 100: _keyX = -0.1; break; // d
        case 97 : _keyX =  0.1; break; // a
    }
}

And it's unchanged between the two window-styles. Is there anything else I ought to be doing in the window mask to indicate that I really do want key-event processing ? Or is it just understood that only some types of window have key processing ?

One other thing - the OpenGL window base class defines its own canvas element and then obtains the Open-GL context from it, using:

/******************************************************************************\
|* Initialiser
\******************************************************************************/
- (id)initWithFrame:(CGRect)aFrame 
{
self = [super initWithFrame:aFrame];
if (self) 
{
_DOMCanvasElement = document.createElement("canvas");
_DOMCanvasElement.width = CGRectGetWidth([self bounds]);
_DOMCanvasElement.height = CGRectGetHeight([self bounds]);
_DOMCanvasElement.style.top = "0px";
_DOMCanvasElement.style.left = "0px";
_DOMElement.appendChild(_DOMCanvasElement);

_gl = [[GLContext alloc] initWithGL:[self _context]];
[self prepareOpenGL];
}
return self;
}

/******************************************************************************\
|* Obtain the context
\******************************************************************************/
- (DOMElement) _context 
{
var gl = nil;
try 
gl = _DOMCanvasElement.getContext("webkit-3d");
return gl;
catch(e) 
{}

try 
gl = _DOMCanvasElement.getContext("moz-webgl"); 
}
catch(e) 
{}

if (!gl) 
{
CPLog.error(@"Could not create context")
return nil;
}
return gl;
}

... so perhaps there's more plumbing that I'd need to do to get events to transport across in the non-full-screen-window case ?

Cheers
   Simon

Martin Carlberg

unread,
Apr 30, 2019, 3:47:33 AM4/30/19
to objec...@googlegroups.com
Hi!

Have you tried to add this to you view?

- (void)keyUp:(CPEvent)anEvent
{
    [[[self window] platformWindow] _propagateCurrentDOMEvent:YES];
}

- (void)keyDown:(CPEvent)anEvent
{
    [[[self window] platformWindow] _propagateCurrentDOMEvent:YES];
}

This should allow the event to be handled by the OpenGL elements. This is just of the top of my head so you need to test it.

Look at CPTextField.j in the Cappuccino framework how that is handled in text fields: https://github.com/cappuccino/cappuccino/blob/master/AppKit/CPTextField.j#L1075


- Martin

--
You received this message because you are subscribed to the Google Groups "Cappuccino & Objective-J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectivej+...@googlegroups.com.
To post to this group, send email to objec...@googlegroups.com.
Visit this group at https://groups.google.com/group/objectivej.
For more options, visit https://groups.google.com/d/optout.

Simon

unread,
Apr 30, 2019, 11:29:37 AM4/30/19
to Cappuccino & Objective-J
Hey Martin :)

So it looks as though I'm not getting any call at all to the -keyDown:(CPEvent) or -keyUp:(CPEvent) handler in the view. So, putting the 

[[[self window] platformWindow] _propagateCurrentDOMEvent:YES];

call in the view's event-handling doesn't do anything. 

Looking at the CPTextField code (and bear in mind Javascript isn't my strong suit - that's kinda why I love Cappuccino :) ... it looks as though the CPTextfield explicitly manages KeyUp events by creating a CPEvent, and sending it to the CPTextfield's owner. Curiously there's no mention of how it handles KeyDown events that I can see...

It would have been nice if it worked in any window, but I think I can get away with just having GL in the first case above (CPBorderlessBridgeWindowMask) so I'll move on to other problems :)

Reply all
Reply to author
Forward
0 new messages