Disable the builtin keyboard in mac os x programmatically? - swift

Is there a way to disable the keyboard in macos x?
I am looking for a way I can use this functinality in an application. So preferably something that can be done programatically and does not need root previlages?
I want the keyboard to be dsabled everywhere in the os, even if the application is in the background, until I enable it by pushing a butten for example.
I am new to mac os programming so will appreciate more elaborations :)
UPDATE: I am trying to use this: CGEventTapCreate but cannot get it to work. Anyone nows how to make it work in swift 3?

You can override the keyDown and keyUp events and leave the function without calling its super class
override func keyDown(with event: NSEvent) {
//super.keyDown(event)
}
override func keyUp(with event: NSEvent) {
//super.keyUp(event)
}
The above code works for NSTextView
For NSTextField, you should manually delete the letter entered. As for NSTextfield's keyDown event will not be called when overrided.
override func keyUp(with event: NSEvent) {
self.stringValue = ""
}
I used these techniques, but not really sure if this is right way to do... But it works :P

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

Prevent ipad from closing the virtual keyboard on keyboard extension

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

Swift Playgrounds aren't acting correctly after Xcode Update

I have been making a Swift playground that uses the touchesBegan method to accept user interaction. It has been working fine, along with the NewtonsCradle and other Swift playgrounds I have downloaded from the internet. But I just updated my Xcode to version 8.3 and NO Swift playgrounds are currently working.
My Swift playground is no longer accepting any user input.
For example I set up a test view and when I click on it "touched" does not print:
class TestView : UIView {
public init() {
super.init(frame:CGRect(x: 0, y: 0, width: 50, height: 50))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touched")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
}
}
var controller = TestView()
PlaygroundPage.current.liveView = controller
PlaygroundPage.current.needsIndefiniteExecution = true
I also tried running the NewtonsCradle code (one updated for Swift 3.0 so that is not the problem) and the balls do NOT respond to user interaction and they just swing on their own. See this picture of them stuck in the up-left position:
Any one have any idea what is going wrong, or what I can do? I am really pressed for time to get this to work. I also cannot roll back to the previous version of Xcode as I need the Playground to work on the latest version.
EDIT: I just updated to MacOS 10.12.4 and it is still broken. Any help?
EDIT 2: I have gotten others to confirm this as an issue for them. Perhaps others can also confirm this and if possible file an Apple Bug Report as well or come up with a fix.
Here is quick fix from Apple Engineers
You just need to quit Xcode and execute following command in terminal:
defaults write com.apple.dt.xcode IDEPlaygroundDisableSimulatorAlternateFramebuffer -bool YES
It helped in my case where all UIPanGestureRecognizers stopped working after updating to Xcode 8.3.
Also note that this will decrease quality of rendered playground.
There are lots of solutions:
Clean Xcode (Shift+Command+K)
Deep Clean Xcode (Shift+Option+Command+K)
Insanely Clean Xcode (delete /Library/Developer/DevrivedData)
If that still doesn't work, reinstall Xcode:
Run:
sudo /Developer/Library/uninstall-devtools
sudo rm -rf /Developer
Then delete Xcode.app from the applications folder.

How to reload an application from a 'play again?' button

Using Xcode 7.3, writing Swift program; searched for this topic but could not find an answer. Apologies, in advance. Last program language was COBOL.
Application background:
It is a single view application, User keys in data and at some point the User 'wins' - at that point, two buttons are available: 'Quit?' and 'Play Again?'
For the 'Quit?' button I have:
// MARK: C.This IBAction used to quit the game
#IBAction func quitIt(sender: AnyObject)
{
exit(0)
}
Hopefully, the above is the most efficient way.
What I need help on is the 'Play Again?' button:
// TODO: B. This IBAction used to play another game
#IBAction func Playagain(sender: AnyObject)
{
}
What code do I put in?
Move the startup code for your game to it's own func(tion).
Call that function both from [wherever you moved it from] and also in PlayAgain().
I hope this is for OS X rather than iOS - Apple will probably reject your iOS app if it exits. They consider that "crashing".
https://developer.apple.com/library/ios/qa/qa1561/_index.html

How to get filenames when you drag'n'drop files from Finder on your app

I'm trying to let my NSView accept files which you can drag and drop from Finder.
I've already checked the documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DragandDrop/Tasks/acceptingdrags.html#//apple_ref/doc/uid/20000993-BABHHIHC), but the only thing that works is accepting a drop of a String.
As the docs say, I have registered the types which can be dropped
func commonInit(){
let allowedDropTypes = [NSFilenamesPboardType]
registerForDraggedTypes( allowedDropTypes )
Swift.print( registeredDraggedTypes )
}
If I print out the registeredDraggedTypes, I get NSFilenamesPboardType.
I've also implemented draggingEntered(sender)
override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation {
Swift.print("Dragging entered")
return sender.draggingSourceOperationMask()
}
And prepareForDragOperation(sender) and performDragOperation(sender)
override func prepareForDragOperation(sender: NSDraggingInfo) -> Bool {
Swift.print("Prepare for drag operation")
return true
}
override func performDragOperation(sender: NSDraggingInfo) -> Bool {
Swift.print("Perform for drag operation")
return true
}
If I run my app, I can't drop any files from Finder on my app.
When I add NSStringPboardType to the array of allowed types, I can drag a piece of text on the app. If I then try to add NSPDFPboardType to the array and drag a pdf on the app, it also doesn't work.
I've also tried working with the UTI public.file-url, but to no avail.
Also, app sandboxing is turned off.
I hope somebody can help me out :-)
I found out what the problem was.
My NSView had NSImageViews as subviews and these were blocking the drag operation.
I've added an invisible NSView subview at the top and registered the drag operation on that view. Now everything works fine.
Can you try to allow/add the filetypes you want in the Info plist of your project?
example info plist file
I think you need to define the type of files you allow your application to open/handle, although not 100% sure if this is related to drag/drop. Worth a try at least.