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

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;
}
}

Related

EntryHandler with PlaceHolder Text above in .Net MAUI

How to Display Entry Control with PlaceHolder Text in MAUI.
I need to write a EntryHandler which would look like the below Image
(Last Name* is the placeholder text)
I have written a entry handler with out the border which is looking like below, the problem with this is as the user starts typing the placeholder text hides
public class BorderlessEntry : Entry
{
}
public App(AppShell page)
{
InitializeComponent();
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping(nameof(BorderlessEntry), (handler, view) =>
{
if (view is BorderlessEntry)
{
#if __ANDROID__
handler.PlatformView.SetBackgroundColor(Microsoft.Maui.Graphics.Colors.Transparent.ToAndroid());
handler.PlatformView.Hint = view.Placeholder;
#elif __IOS__
handler.PlatformView.BackgroundColor = Microsoft.Maui.Graphics.Colors.Transparent;
handler.PlatformView.Layer.BackgroundColor = Microsoft.Maui.Graphics.Colors.Transparent;
handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
#endif
}
});
}
I have been able to achieve the same in Xamarin Forms using MaterialEntryRenderer but the same is not supported in .net Maui
Any help is appreciated!

How to get text input continuously in Unity C#?

I'm going to get text input using TextField and the following is my code:
public Text textInput;
void Update()
{
if (Input.GetKeyDown(KeyCode.Return)) {
if (textInput.text == "go") {
/* do something */
textInput.text = "";
}
}
}
I assigned the Text element under TextField to public Text textInput, and I expected that once a player types "go" and presses enter, the TextField will be cleared and the player be able to type next sentence.
However, after I type "go" and press enter, the text in TextField remained and the focus was out.
How can I clear the TextField maintaining the focus on it?
I think the "problem" is that the input field by default also handles the Return key as the submission command which automatically loses the focus.
The default InputField.lineType is
SingleLine
Only allows 1 line to be entered. Has horizontal scrolling and no word wrap. Pressing enter will submit the InputField.
You could instead set it to MultiLineNewLine
Is a multiline InputField with vertical scrolling and overflow. Pressing the return key will insert a new line character.
So it doesn't automatically call the OnSubmit and lose focus when pressing the Return key.
Using that as an alternative to use Update and GetKeyDown you could use InputField.onValidateInput and do something like
public InputField textInput;
private void Awake ()
{
textInput.lineType = InputField.LineType.MultiLineNewLine;
textInput.onValidateInput = OnValidate;
}
private char OnValidate(string text, int charIndex, char addedChar)
{
if(addedChar == '\n')
{
if(text.Equals("go"))
{
// Do something
}
textInput.text = "";
return '';
}
return addedChar;
}

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

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?

How to validate Text mesh pro input field text in unity

I have tried to use text mesh pro input field in my project but I have face one series issue with that. i.e If try to validate empty or null text in input field it fails. For example when user without typing any text in tmp input field and click done button I have set a validation like not allowed to save null empty values but when user click done button without typing any text, those validations are fails. Please suggest any idea to fix this issue. Thanks in advance.
Here is the code I have tried :
var text = TextMeshProText.text; // here "TextMeshProText" is 'TMP_Text'
if(!string.IsNullOrEmpty(text))
{
//do required functionality
}
else
{
// Show alert to the user.
}
I have set the validation like this but without giving any text click on done button it fails null or empty condition and enter in to if.
I found the problem. It fails because you use TMP_Text instead TMP_InputField.
Note that: Use the code for TMP_InputField; not for TMP_Text that is inside it as a child.
Change your code to this:
TMP_InputField TextMeshProText;
...
public void OnClick ()
{
var text = TextMeshProText.text; // here "TextMeshProText" is 'TMP_InputField'
if (!string.IsNullOrEmpty(text))
{
//do required functionality
}
else
{
// Show alert to the user.
}
}
I hope it helps you

ckeditor + smartgwt modal window + dialog dropdown gains focus but does not show options

I am using the ckEditor along with GWT and SmartGWT. I have a problem that whenever the ckEditor displays a dialog (e.g. link button, table button), although the items in the dialog gain focus (input texts work fine, I can write inside them), the dropdowns (select elements) when clicking on them, do not expand to show their option items (they expand only when they have focus and user hits "spacebar"). This happens only in firefox and chrome (latest versions) while on IE11 it works as expected.
Note that I am already aware of the "focus" problem existing if a ckEditor instance exists in a GWT/jquery modal and I have already included a fix:
$wnd.CKEDITOR.on('dialogDefinition', function (evt) {
var dialog = evt.data.definition.dialog;
dialog.on('show', function () {
var element = this.getElement();
var labelledby = element.getAttribute('aria-labelledby');
var nativeElement = $wnd.document.querySelector("[aria-labelledby='" + labelledby + "']");
nativeElement.onclick = function (evt) {
if ((evt.target.tagName == "INPUT" || evt.target.tagName == "SELECT" || evt.target.tagName == "TEXTAREA") &&
-1 != evt.target.className.indexOf("cke_dialog_ui_input")) {
evt.target.focus();
};
}
});
});
Any hint how I can make the dropdowns to behave correctly? To me it looks like the dropdown element does not receive the click event (although on click it gets focus) or somehow the event's propagation stops unexpectedly.
EDIT
Forgot to mention that the problem appears if the ckEditor instance is inside a modal SmartGWT window. More specifically if I set
Window win = new Window(); //com.smartgwt.client.widgets.Window
win.setIsModal(false);
and then add the DynamicForm form which contains the ckEditor item on that window then the dialog dropdowns work fine, however if I set
win.setIsModal(true);
I get the faulty behavior described above
In case anyone else has the same problem with me, the solution is to call win.hideClickMask() upon show event of the dialog. This can be achieved in many ways depending on how ckEditor is integrated with SmartGWT. In my implementation this is achieved by overriding onDialogShow() as below:
final CKEditor ckEditor = new CKEditor(conf) {
#Override
public void onDialogShow() {
// to overcome the problem that smartgwt modality obstruct the dropdowns of a ckeditor dialog to be pressed
final NodeList<Element> allWindowsWithModalMask = findAllWindowsWithModalMask();
if(allWindowsWithModalMask != null ) {
for(int i =0; i<allWindowsWithModalMask.getLength(); i++) {
Element el = allWindowsWithModalMask.getItem(i);
String id = el.getAttribute("eventproxy");
if(Canvas.getById(id) != null) {
hideClickMask(Canvas.getById(id).getOrCreateJsObj());
}
}
}
}
};
and
protected native NodeList<Element> findAllWindowsWithModalMask() /*-{
return $wnd.document.querySelectorAll("[class='windowBackground']");
}-*/;
protected native void hideClickMask(JavaScriptObject windowCanvas) /*-{
windowCanvas.hideClickMask();
}-*/;