Swift 5.1 console app in Linux : keyboard arrow Up/Down behaviour - swift

I'd like to add bash-like behaviour for my console app - when I press Arrow Up/Down I can see the previous commands entered. Here is the way I handle user input :
while true {
print(">> ", terminator : "")
if let inputCli = readLine() {
let lowerCasedInput = inputCli.lowercased()
if lowerCasedInput == "exit" {
break
}
}
}
What is the way to achieve this?
If I press Up and Down I see the following output :
^[[A^[[B
How can I handle these press events?

Related

Using Hotkey If, With EvilC TapHoldManager Library

Lately I have been using Autohotkey to help me get past my left hands injury and the pain I get when I "push it" by reaching out for keys, especially, when my other hand is on the mouse
I recently had this brilliant idea that will let me eliminate reaching out for keys, by using a keypad instead of a full sized keyboard.
My idea is to use a VIM style hotkey\editing (The Linux\text editing tool) design.
when I tap NumpadSub, then first tap of NumpadMult should perform a special action. After this first tap of NumpadMult future taps should perform the default windows function, as if no AHK scripts were running.
The thing is, to make up for the small number of keys available, I want to use EvilC's TapHoldManager library to achieve this. I have spent the past few days increasing my knowledge of this library.
Using THIS post and u\ExpiredDebitCard answer HERE I came up with this:
#include <TapHoldManger>
THMLeader:= new TapHoldManager(200,400,2)
THMLeader.Add("NumpadSub", Func("LeaderKey__NumpadSub"))
THMLeader.Add("NumPadMult", Func("LeaderKey__NumPadMult"))
LeaderKey__NumpadSub(isHold, taps, state)
{
if (taps == 1)
if (isHold == 0)
{
Tooltip, THMLeader Tap x1
TestFlag := 1
}
else
{
if (state)
{
Tooltip, THMLeader Hold x1
}
; else
; {
; }
}
}
LeaderKey__NumPadMult(isHold, taps, state)
{
if (taps == 1)
if (isHold == 0)
{
Tooltip, LeaderKey__NumPadMult Tap x1
}
else
{
if (state)
{
Tooltip, LeaderKey__NumPadMult Hold x1
}
; else
; {
; }
}
}
TestFlag := 1
Hotkey If,TestFlag == 1
;Hotkey If,(TestFlag == 1)
THMSubKey:=New TapHoldManager(200,400,2)
THMSubKey.Add("NumpadMult", Func("SubKey__NumPadMult"))
#If
SubKey__NumPadMult(isHold,taps,state)
{
if (taps == 1)
if (isHold == 0)
{
Tooltip, SubKey__NumPadMult Tap x1
TestFlag := 0
}
else
{
if (state)
{
Tooltip, SubKey__NumPadMult Hold x1
TestFlag := 0
}
; else
; {
; }
}
}
My intention with the above code is that, SubKey__NumPadMult code block should only ever be active if variable TestFlag is set to 1. Only single tap of NumpadSub can set TestFlag to 1. so
So in short,
A single tap of NumpadSub followed by NumpadMult will print SubKey__NumPadMult Tap x1.
A single tap of NumpadMult NOT preceded by a tap of NumpadSub will print LeaderKey__NumPadMult Tap x1
I am having issues with getting Hotkey If,TestFlag == 1 to work though. Running the above code gives me a runtime error:
Error: Parameter #2 must match an existing #If expression.
Changing it around like. Hotkey If,(TestFlag == 1) has not helped either.
What am I doing wrong here? I have tried many things, nothing seems to be giving in. Any help or ideas would be great.
Thanks for any help

How to get character entered on Keydown Event in WPF C#

i'm using wpf c# in visual studio
i want to prevent user can enter Arabic character , Just Persian Character
like when user entered this value on keyboard → "ي" change it to "ی"
my means something like this :
when user press button to type "A" on keyboard i want to change this character, first check if is "A" change to "B"
i did it in Windows Form Application , but that code does not work in WPF
My Code in Windows From :
if (e.KeyChar.ToString() == "ي")
{
e.KeyChar = Convert.ToChar("ی");
}
My Code in WPF :
if (e.Key.ToString() == "ي")
{
e.Key.ToString("ی");
}
These codes not working in WPF
Please Help
It's a bit different in WPF.
This works using a english keyboard. Don't know if it will work with Arabic, as the rules might be a little different for inserting characters.
You can try handling the PreviewTextInput event of the TextBox.
XAML:
<TextBox PreviewTextInput="TextBox_OnTextInput" ...
Code:
private void TextBox_OnTextInput(object sender, TextCompositionEventArgs e)
{
var box = (sender as TextBox);
var text = box.Text;
var caret = box.CaretIndex;
if (e.TextComposition.Text == "ي")
{
var newValue = "ی";
//Update the TextBox' text..
box.Text = text.Insert(caret, newValue);
//..move the caret accordingly..
box.CaretIndex = caret + newValue.Length;
//..and make sure the keystroke isn't handled again by the TextBox itself:
e.Handled = true;
}
}

How do I dismiss OSX Character Picker Palette from code

I can present the Character Picker Palette (emoji) with:
NSApplication.shared.orderFrontCharacterPalette(self)
But I can't find any way to dismiss the Character Picker Palette from code when I'd like to.
I have tried iterating through all open windows, and closing all but the main window like this, but although my print statement reports 2 other windows, it does not close the character picker palette :
let openwindows = NSApplication.shared.windows
for win in openwindows {
if win == self.view.window {
print("Found and ruled out the main window") // successfull
}else{
print(win.className)
// finds two other windows and prints
// _NSCandidateBarCorrectionPanel
// NSCorrectionSubPanel
//
win.close() // Has no effect
// win.orderOut(self) // Has no effect
// win.performClose(nil) // sounds system beep
}
}

Best way to count length of UITextInput text value in Swift?

I have two UITextInput controls that I want to count the number of characters in.
For context, I have two inputs: one for email address and one for password. I also have a “Login” button. The button is inactive by default, but as soon as at least one character is entered into both inputs, I’ll programatically enable the button. This is to prevent login attempts until the user has entered a value in both fields.
So far, I’m using this approach:
if count(emailInput.text) > 0 && count(passwordInput.text) > 0 {
// Enable button
} else {
// Disable button
}
Is using the count() function acceptable? Or is there a better way? As I recall there are some gotchas with checking the length of strings in iOS/Swift.
For me personally the following code has worked fine in past.
if (!emailInput.text.isEmpty && !passwordInput.text.isEmpty) {
// enable button
} else {
// disable button
}
if((emailInput.text!.characters.count) > 0 && (passwordInput.text!.characters.count) > 0) {
// Enable button
} else {
// Disable button
}
Swift 4
emailInput.text!.count
Swift 2
self.emailInput.text!.characters.count
but if you need to do something more elegant you can create an extension like this
Nowadays, after putting count directly on text property my guess is to use this directly without extension, but if you like to isolate this sort of things, go ahead!.
extension UITextField {
var count:Int {
get{
return emailInput.text?.count ?? 0
}
}
}

Clicking keyboard 'Next' key using UIAutomation

I have a search field in my app and I have set the return key type of the keyboard for this field to UIReturnKeyNext. I am attempting to write a UIAutomation test that clicks the Next button on the keyboard using the following line:
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().keys().firstWithName("next");
This call is failing because the key with name 'next' is not being found. I have done a dump of all of the elements in my app using:
UIATarget.localTarget().frontMostApp().logElementTree();
This reveals that there is indeed a key in the keyboard with name 'next', but somehow my attempt to retrieve it as show above still fails. I can however retrieve other keys (like the key for the letter 'u') using this method. Is there a known issue here or am I doing something wrong?
I've tried other variations with no luck:
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().elements()["next"];
Here is a screen capture of the elements in my UIAKeyboard:
If you just want to click it, and you know the keyboard has "next" as "Return key" (defined in your nib), then you can use this:
app.keyboard().typeString("\n");
Jelle's approach worked for me. But I also found an alternative way if anybody needed it.
XCUIApplication().keyboards.buttons["return"].tap()
Where you can create XCUIApplication() as a singleton on each UI Test session. The thing about this approach is you can now distinguish between return and done and other variants and even check for their existence.
You can go extra and do something like following:
extension UIReturnKeyType {
public var title: String {
switch self {
case .next:
return "Next"
case .default:
return "return"
case .continue:
return "Continue"
case .done:
return "Done"
case .emergencyCall:
return "Emergency call"
case .go:
return "Go"
case .join:
return "Join"
case .route:
return "Route"
case .yahoo, .google, .search:
return "Search"
case .send:
return "Send"
}
}
}
extension XCUIElement {
func tap(button: UIReturnKeyType) {
XCUIApplication().keyboards.buttons[button.title].tap()
}
}
And you can use it like:
let usernameTextField = XCUIApplication().textFields["username"]
usernameTextField.typeText("username")
usernameTextField.tap(button: .next)
I dont't have an example to test, but as the "Next" button is an UIAButton, and not an UIAKey you could try :
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons()["next"];
If it doesn't work, you can also try
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons()[4];
The following works for me:
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWi‌​thPredicate("name contains[c] 'next'");
For me, keyboard does not fall under mainWindow() in the View Hierarchy. It is at the same level as mainWindow() when you logElementTree() from top level. So, what you want to do is:
UIATarget.localTarget().frontMostApp().keyboard().buttons()["next"];
This worked for me when I was trying to press the "Search" button on keyboard.