WPF USB card reader keyDown - c#

I have problem with reading text from card reader connected to USB.
I have method in window:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
Key k = (Key)e.Key;
textBoxLogin.Text += k.ToString();
}
Problem is that, it simulates all pressed keys so if in my magnetic card i have something like:
!EXAMPLE,
that would read LeftShift1LeftShiftELeftShiftX etc..
Any solution how to change it?
Btw i know i can click on textBox and then read all from card reader, but that should work with disabled textBox.
Thank u for any answers!

I guess there is no easy way to convert keys to a string. If you really want to get the text from the Window_KeyDown callback, I think you'll have to code your own converter.
The source of your problem comes from the input : why does the card contains a series of keydown events rather than directly the characters ?

Related

make cursor just focuses on textbox in c#

I have a textbox in my c# application that gets the code from RFID Reader The RFID reader is connected to my computer using USB port .So My textbox should always be aware to read the data from RFIDReader because i can't lose the data .
The RFID Reader is plug and play .and it works like a keyboard and reads the data from the RFID card and returns the serial .
My solution is just create a textbox to get the received data .But how can i sure that every time the cursor is focused on my textbox to get the data .Do you have better solution if No My question is :
how can i sure that every time the cursor is focused on my textbox to get the data?
maybe the better solution is get the data from the socket ?!
To make your textbox always the focused control, set the focus back when it's lost:
textBox1.Leave += (o, e) => { textBox1.Focus(); };
You should write this code into the constructor after InitializeComponent.

Barcode scanner with a WPF application

I have a barcode scanner (bluetooth) connected to my computer to use for scanning some barcodes. The scanner acts exactly like a keyboard does and returns whatever it scans. In my WPF application I have some textboxes for the user to manually enter in a product number, revision number, bin number, and lot number.
I would like the user to be able to instead scan the QR/Bar? code which has all that information in it at any time. So I have a few issues:
Is it possible to scan a barcode with your focus on anything in the app and NOT write it out like a keyboard? Example, I have a random textbox highlighted right now but I go and scan a code - I dont want the code to fill in this random textbox - I would want something like all the scanned text goes into a variable.
If step 1 is not possible. The way I currently have it set up is you need to click into a specific textbox. Which on TextChanged will parse it and try to figure out where the pieces need to go. But it will trigger when each character is added to the box. I have about ~30 characters per code so this will slow it down tremendously. I tried to see what other event might work but I don't see any other solution to that issue? Is there some other event that I'm just missing?
Thank you
I dont want the barcode to fill random textboxes - I want all the scanned QR/barcode into a variable.
private string barCode = string.Empty; //TO DO use a StringBuilder instead
private void Window_Loaded(System.Object sender, System.Windows.RoutedEventArgs e)
{
MainWindow.PreviewKeyDown += labelBarCode_PreviewKeyDown;
}
private void labelBarCode_PreviewKeyDown(object sender, KeyEventArgs e)
{
if ((44 == e.Key)) e.Handled = true;
barCode += e.Key;
//look for a terminator char (different barcode scanners output different
//control characters like tab and line feeds), a barcode char length and other criteria
//like human typing speed &/or a lookup to confirm the scanned input is a barcode, eg.
if (barCode.Length == 7) {
var foundItem = DoLookUp(barCode);
barCode = string.Empty;
}
}
See update, as at March 2019:
https://stackoverflow.com/a/55411255/495455

SendKey command working with vbs but not C#

I am trying to send Ctrl+Alt+Left from a windows form button (C#) to rotate my screen. It is an intel hotkey.
When I press the button nothing is happening, but if I use almost the same command in a vbs file it works.
This is the c# code which does not work:
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("^%{LEFT}");
}
vb script which does work:
Set objShell = CreateObject("WScript.Shell")
objShell.SendKeys "^%{LEFT}"
I'm very new to all this so maybe I'm missing something obvious?
maybe is a parenthesis issue:
SendKeys.Send("^(%{LEFT})");
you can find more details in the documentation where is stated that:
To specify that any combination of SHIFT, CTRL, and ALT should be held
down while several other keys are pressed, enclose the code for those
keys in parentheses. For example, to specify to hold down SHIFT while
E and C are pressed, use "+(EC)". To specify to hold down SHIFT while
E is pressed, followed by C without SHIFT, use "+EC".
not sure about the behaviour of VBS: i would expect the very same behavior of C# because the VBS documentation for SendKeys say so...

c# sending control shortcuts

I am making a program in c sharp/xaml. I have a save button, and figured the easiest way to make it effective was when pressed if I could send the "control s" signal. What is the command (and any includes VS wouldn't add as standard) to do so?
A completely different question to cut down on thread count, how would I make a textblock (or textbox if easier) automatically newline when you reach the end rather than continuing to send text offscreen.
There are better patterns that I would suggest looking into before coupling your UI to keypresses (such as Commands) but if you really want to do this, you can use the SendKeys class from windows forms. This will allow you to send key presses to the application as if the user pressed those keys.
As for the second question, if you're using WPF just create a textbox element and set AcceptsReturn="True" and TextWrapping="Wrap". Here's an example:
<TextBox
Name="tbMultiLine"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"
>
This TextBox will allow the user to enter multiple lines of text. When the RETURN key is pressed,
or when typed text reaches the edge of the text box, a new line is automatically inserted.
</TextBox>
You can use the SaveFileDialog() .
private void button1_Click(object sender, EventArgs e)
{
// When user clicks button, show the dialog.
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
// Get file name.
string name = saveFileDialog1.FileName;
// Write to the file name selected.
// ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test");
}
If you're using XAML I'll assume you're using WPF. So you should be able to bind the Command property of the Button to ApplicationCommands.Save.

C# textbox deletion question

I am working on a project that I use textbox as telnet terminal.
The terminal has "->" as the command prompt in the textbox.
Is there a way to disable the delete or backspace once it reach the "->" prompt?
I don't want to delete the command prompt.
Thanks
Dave is right.
The best way to do this is to make a label on the left side of the textbox that says ->.
You can remove the textbox's border and put them both in a white (or non-white) box to make it look real.
This will be much easier for you to develop and maintain, and will also be more user-friendly. (For example, the Home key will behave better)
Two options:
Make the prompt ("->") an image or label, instead of being part of the textbox.
If it's a web app, handle the textchanged event in javascript and cancel the textchanged if it represents a deletion of the prompt. If its not a web app, do the same thing in c# rather than JS.
You could always make sure that when deleting, the index of the character you're deleting is > 1 (since -> would occupy positions 0 & 1)
This is a naive example, but you should be able to figure it out from here. You can peak at the keydown event and cancel it, when desired.
private void testTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back && testTextBox.SelectionStart == 2)
{
e.SuppressKeyPress = true;
}
}

Categories