Prevent ipad from closing the virtual keyboard on keyboard extension - swift

i know this is a designed part of the ipad to close the keyboard when the hardware keyboard is connected (which make sense). I understand but since ipadOS 15, the virtual keyboard will hide everytime a key is pressed on the hardware keyboard. This was not the case before, you could open it back and use it. We have a prediction keyboard extension which help childrens suffering dyslexia to write. I can't seem to find any documentation on this but is there a private API or something to prevent a virtual keyboard from closing inside a keyboard extension. I can see 'Grammarly' app doing it so it is possible.
So far i can detect if a hardware keyboard is connected using the GameController SDK :
let isKeyboardConnected = GCKeyboard.coalesced != nil
This is not working too inside a keyboard extension
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return false
}
I need to see my virtual keyboard at the same time as typing with the hardware keyboard in order to see the prediction view.
Thanks

Related

Detecting a physical keyboard from a keyboard extension swift ipadOS

i can't seem to find any documentation on how to detect the physical keyboard connection (bluetooth) inside a keyboard extension application. Our keyboard extension is helping children suffering dyslexia how to write. I need to detect if a physical keyboard is attached so i don't show the whole virtual keyboard too. I know it's possible because only one application is doing it perfectly and it's called 'Grammarly'. The only thing i tried so far is overriding the pressesBegan function which is not called in a keyboard extension.
func pressesBegan(_ presses: Set<UIPress>,
with event: UIPressesEvent?)
It must be something custom i guess.
You can detect physical keyboard on iOS 14 GameController SDK by using public API GCKeyboard. You just need to import GameController
let isKeyboardConnected = GCKeyboard.coalesced != nil

typeText() is typing inconsistent characters

I'm beginning to dip my toes into Swift alongside writing UI tests and am having a problem with typing text into a textField. Below is the code:
func testLoginUsernameField() {
let app = XCUIApplication()
app.launch()
let username = "testusername2"
let usernameField = app.textFields["username_field"]
XCTAssertTrue(usernameField.exists)
usernameField.tap()
usernameField.typeText(username)
XCTAssertEqual(usernameField.value as! String, username)
}
The problem occurs when I do usernameField.typeText(username). My text continues to write tstusername2 rather than the testusername2.
This issue happens on the simulator when the Hardware Keyboard is enabled.
Disable the hardware keyboard via the menu
Go to I/O -> Keyboard -> Uncheck "Connect Hardware Keyboard" or use the shortcut ⇧⌘K.
Disable the hardware programmatically
If you'd like to disable the hardware keyboard for your Scheme, no matter what simulator you run, refer to this StackOverflow post. I attempted to use other methods to disable the hardware keyboard via the App Delegate but had no luck.

After hiding NSRunningApplications some automatically unhide

Im using the following code to hide all the running applications whose activationPolicy == .regular and if the bundle identifier is different from my app.
NSWorkspace.shared.runningApplications
.filter { runningApps.contains($0.bundleIdentifier) }
.forEach { item in
item.hide()
}
Right after hiding them some just automatically unhide, apps that usually have a search bar or first responder textview like telegram, finder, messages. It's random and sometimes it reappears, sometimes not...
I also tried with NSWorkspace.shared.hideOtherApplications() but it does the same
macOS 10.14.3
Any idea on what should I do before hiding?

Global keyboard shortcuts for a Mac tray app

I am developing a Mac app using Swift 4 which only runs as an Agent (it only has a menu in the tray). I'm completely new to Swift.
I want it to respond to keyboard shortcuts, but the basic shortcuts (see picture) only work when the app has focus (in my case, when the menu is being clicked).
So what I need is for my app to respond to system-wide keyboard shortcuts.
I've tried:
HotKey but it doesn't seem to be compatible with Swift 4, or perhaps I can't use Swift Package Manager
this answer but again Swift 4 raises several errors that I couldn't solve
Has anyone done it?
So I ended up getting it to work with HotKey:
by using Carthage as a package manager instead of SPM
and by not using HotKey's example code from their README, which for some reason didn't work for me, but the one in their example app.
It works great!
I made a Swift package that makes this very easy:
import KeyboardShortcuts
extension KeyboardShortcuts.Name {
static let startFiveRounds = Self("startFiveRounds", default: .init(.t, modifiers: [.command, .option]))
}
#main
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
KeyboardShortcuts.onKeyUp(for: .startFiveRounds) {
// …
}
}
}
Even though you already solved your problem, I think this could be useful for other people having the same problem.
I would also recommend letting the user pick their own keyboard shortcut instead of hardcoding it. My package comes with a control where the user can set their preferred shortcut.

display soft keyboard (iPad) when is connected a bluetooth input device

I'm really bangin' my head because I can't find the way to show the soft keyboard when there's a bluetooth input device connected to the iPad. I made some search on the web and this is the result:
a question on stackoverflow with a very short answer How can I detect if an external keyboard is present on an iPad?
an application developed by erica sadun for the cydia env http://www.tuaw.com/2010/06/02/hacksugar-bringing-back-the-on-screen-keyboard/
Erica said that the trick is to answer to the system that "There's no hardware keyboard attached".
I tried to write a category for UIKeyboardImpl and I overrided:
- (BOOL)isInHardwareKeyboardMode {
DEBUG(#"is called");
return NO;
}
But until now I haven't obtained anything. The overrided method is called but there's no soft keyboard.
Erica also said the application works by dynamic linking but I don't know how can I accomplish it. I don't need to be in the AppStore because this is a private application so I don't bother about rejection.
Thanks in advance
Ok. Finally got it. Many thanks to David, Matthias and Enrico. Here are the steps:
import the private framework GraphicsServices
call GSEventSetHardwareKeyboardAttached(NO) inside the viewDidLoad
add a button that toggles the keyboard by calling
static void toggleKeyboard(UIKeyboardImpl * keyImpl){
if (UIKeyboardAutomaticIsOnScreen()) {
UIKeyboardOrderOutAutomatic();
} else {
UIKeyboardOrderInAutomatic();
}
I've found this function on http://code.google.com/p/btstack/wiki/iPhoneKeyboardHiding
Now I can take input from the soft keyboard and from the bluetooth device at the same time.
To get around it using the apple keyboard you hit the eject key. Perhaps you can implement an action that sends the eject keycode? I think iSSH has a feature where you can tap the onscreen keyboard icon to bring it up even when a bluetooth keyboard is connected.