iOS Question iOS keypress override

simonwilliamson

Member
Licensed User
Is it possible to utilize the iOS function
C:
override func pressesBegan(_ presses: Set<UIPress>,with event: UIPressesEvent?)
from within B4i so that my app can pickup physical keyboard key presses?
 

simonwilliamson

Member
Licensed User
Ideally it would be the root of the B4xMainPage (B4XView) although no controls on the 1 page that require this feature have 'focus' so it could be something else if required. Currently in B4i I am having to monitor the TextChanged handler of a textbox hidden off screen to pick up key presses which means I can't pick up non-visible characters like arrow keys and has recently started causing a lag in keypress responses for some reason.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I haven't tested it with physical keyboard, so please try it yourself:
B4X:
#if OBJC
@end
@interface B4IPanelView  (presses)
@end
@implementation B4IPanelView  (presses)
- (void)pressesBegan:(NSSet<UIPress *> *)presses 
           withEvent:(UIPressesEvent *)event {
    NSLog(@"presses: %@", presses);
}
#End If

It adds this event to all panels. Check the logs.
 
Upvote 0

simonwilliamson

Member
Licensed User
Indeed it does work, thank you!
However is it possible to apply the trigger to a specific panel (or control) rather than all the application panels as it stops keypresses going to all the textboxes and therefore nothing can be typed into them.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Private Sub Root_ButtonPresses(Presses As Object)
    Log("event: " & Presses)
End Sub

#if OBJC
@end
@interface B4IPanelView  (presses)
@end
@implementation B4IPanelView  (presses)
- (void)pressesBegan:(NSSet<UIPress *> *)presses 
           withEvent:(UIPressesEvent *)event {
    NSLog(@"presses: %@", presses);
    [B4IObjectWrapper raiseUIEvent:self :@"_buttonpresses:" :@[presses]];
    [super pressesBegan:presses withEvent:event];
}
#End If
 
Upvote 0

simonwilliamson

Member
Licensed User
Hi Erel,
The typed characters are now appearing in the textboxs but the Root_ButtonPresses event never triggers. Is there some code I can put in the NSLog message that will print the event's name just in case it isn't Root?
Also this new code seems to quadruple the NSLog message every time a key is pressed.
 
Upvote 0

simonwilliamson

Member
Licensed User
The event is probably intercepted by multiple views.

B4X:
NSString *eventName = [B4IViewWrapper getEventName:self];
NSLog("event: %@", eventName);

Just a small typo in the code as I think the 2nd line is missing a @ and should read: NSLog(@"event: %@", eventName);
 
Upvote 0
Top