im doing some work on a project that i need to pass my final exam.
So i choosed C#, Windows Forms, and i have something in my mind.
I need to help with event where i press keys on keyboard that represent word.
(like h-e-l-l-o) and if the keys will be pressed in this order something will happen, that i can figure out by myself, but i need help with the keypressed method or something.
TL;DR: I need help with event on my WinForm app that will work like when you type "AWESOME" to nowhere on youtube.
Here is some code from a project. Hook a function to the KeyPress event of the textfield. you can get the full contents inside the function or just the last pressed character.
private void IsKeyPressNumber(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!(e.KeyChar >= '0' && e.KeyChar <= '9'))
e.Handled = true;
}
this.txtCode.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IsKeyPressNumber);
Related
I'm new to WPF and C# so what I'm asking is if there is a backspace event like TextChanged event for TextBoxes?
I made a small Library program with renting books and everything is viewed at a ListView.
What I currently did is that you can filter book names just by typing inside the textbox, so if you have 1000 books and you type the letter 'b' then you might have only 150 books starting with 'b'.
The problem is whenever i press backspace, I want it to previously restore it to what it was.
For example: typing "bob" and then deleted b, I get bo and now i want to present what every starts with "bo".
Now I get the idea. All I need is just another textChanged event. but something need to inform that the text was changed, and I need something better then
if (backspace key is pressed) { Invoke textChanged }
Thx guys!
Well, should i delete the post? maybe some one else will search it someday.
backspace is actually causing a TextChanged event automatically! damn. thx anyway!
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx
Here is a reference on the msdn site.
http://csharp.net-informations.com/gui/key-press-cs.htm
On a different site (easier to read) This though looks like it is for Win Forms.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
if (e.KeyChar == 13)
{
MessageBox.Show("Enter key pressed");
}
}
Looks like you need to create an event that fires on a key down, then get the value of that key. I think there is a Keys.Backspace but to know for sure let intellisense help you.
I'm new here, and I may be blasted for not seeing the old posts about Keys, but I assure you I have read many of them and cannot find the answer I am looking for.
I have a C# program, a calculator, that correctly calculates equations, but I want to be able to call methods by both clicking and keyboard input. Like so if user types in 2 + 2 ENTER the textbox will show 4. The only way the program does that at the moment is if the user actually clicks those buttons. Researching how Keys work in C# I found a lot of information about KeyCode and Keys, but very little information about how to actually implement them within the program. One thing that I came across concerning implementing the code was this:
private void Calculator_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
MessageBox.Show("Up button was pressed");
}
}
The name of the form is Calculator. I can compile this code without a problem, but it doesn't seem to do anything. I can place the entire program for you guys if you want, but it is several pages long and has a lot of comments and other stuff. I just don't understand why I can't make my program read a key. I also tried KeyPress instead of KeyDown, still nothing. Any help would be much appreciated.
Edit:
Marcel N. gave a good answer and I was able to get it to work after enabling the KeyPreview and using the code:
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
cmd1.PerformClick();
if (e.KeyCode == Keys.D2)
cmd2.PerformClick();
if (e.KeyCode == Keys.D3)
cmd3.PerformClick();
if (e.KeyCode == Keys.D4)
cmd4.PerformClick();
......................
}
I am very happy that all the numbers work, but am still having issues getting Enter, Divide, Multiply, and the arrow keys to work. Thanks for the speedy help.
You need to set the KeyPreview property on the form to true.
This will cause all key events to be passed to the form first, before they are delegated to the focused control. Then, you can use your current KeyDown handler to call your methods/handlers.
Finally, if you don't want the key events to reach the focused control at all then make sure to set the KeyPressEventArgs.Handled property to true when you receive the event at the form level.
It seems that the keydown event does not handle spacebar presses unless the control has focus. How do I, though?
I am using c# and I am making a windows store app, if it matters.
I'm not sure of what code you have within your project already but I'd recommend some JQuery along the lines of:
$(window).keypress(function(e) {
if (e.keyCode == 0) {
console.log('Space pressed, here is my event');
}
});
As the event is bound to a Window event, it will find it regardless of whether an input field is focused or not.
I figured it out.
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
captures all key events.
I'd like to detect if a keystroke would result in a closing square bracket ] in a KeyDown event of a WPF Popup:
private void MyPopup_KeyDown(object sender, KeyEventArgs e)
{
if (theKeyStrokeWouldResultIsAClosingSquareBracket)
{
// ...
// do stuff
// ...
e.Handled = true;
}
}
The solution has to work for any keyboard layout. Notice that key mappings of different layouts usually differ quite a lot. Therefore checking for the right key combination (like Ctrl + Alt + Digit 9 on a German QWERTZ keyboard or simply OemCloseBrackets on a US QUERTY keyboard) is unfortunately no solution.
How can you accomplish such a detection despite these problems?
EDIT - Adding some information before starting the bounty:
I'm currently implementing an IntelliSense-like function in my current project. The IntelliSense stuff is inspired by this: http://www.codeproject.com/Articles/22803/Intellisense-like-Method-Selection-Pop-up-Window
The custom syntax allows expressions like [abc].[def] > [ghi].[jkl], where an user should be able to make the intellisense function autocomplete the text in the brackets. To make the intellisense function a bit more similar to Visual Studio's (and lots of other IDEs') behavior, I'd like to enable the user to type a closing square bracket in the popup's list box to force an autocomplete.
You should use the PreviewTextInput event - this gets a TextCompositionEventArgs argument whose Text property will be the closing bracket.
You could add a transparent TextBox to the popup, and subscribe to the TextChanged event on that instead of an event on the Popup.
If it's a key combination I don't think you'll be able to capture it using a key down event.
if (e.Key == Key.OemCloseBrackets)
{
}
is the correct approach for the simple case with an English keyboard. If you would like to cope with the German keyboard that you have described, you should do a string comparison on the text box that you are working with calling from the key up event.
if (txtKeypad.Text.Substring(txtKeypad.Text.Length - 1, 1) == "]")
I hope this helps.
Ed
I have a custom search activity that serves search suggestions. I get a reference to the search input box and set a handler:
mSearchInput = FindViewById(Resource.Id.searchBoxInput) as EditText;
mSearchInput.KeyPress += new System.EventHandler<View.KeyEventArgs>(OnSearchKeypress);
This is the handler that should be called on every key press:
private void OnSearchKeypress(object sender, View.KeyEventArgs e) {
string query = mSearchInput.Text;
if (e.KeyCode == Keycode.Enter && !string.IsNullOrEmpty(query)) {
e.Handled = true;
// launch search results activity
}
else {
e.Handled = false;
if (query.Length > 2) {
// load suggestions if we have 2+ characters
}
}
}
For some reason, on the soft keyboard, only certain key presses fire the event at all. Enter and Delete fire the event. Alphanumeric keys do not. Even if I take out all of the logic and simply do a Toast with e.KeyCode.ToString() it only shows the toast on those few keys.
Am I listening for the wrong event, is this a bug, or have I made some other mistake?
Device: Droid X2
Mono Version: 4.0.4
Update: I discovered that KeyPress is triggered twice for each physical press of a key: once for KeyEventActions.Up and once for Down. This doesn't explain why only certain keys fire the event but it should be noted that you need to check event.E.Action to find out whether the press or release is currently triggering the event.
When is the event not firing? If it is when the user is typing, then you can use the TextChanged event. You can then continue with your implementation in the OnKey method.
Also, look at this article for the "search" button:
http://www.stackoverflow.com/questions/1489852/android-handle-enter-in-an-edittext