Hi,
I'm creating an application where I need to enter commands from the keyboard so I was struck in assigning keys like UP and DOWN arrow
I tried something like this but it did not work
if(e.keycode == keys.up)
{
this.moveup();
}
I guess there is something wrong in this could you please guys help me to resolve this issue..
Keyboard shortcutsI wanted to use up and down arrow keys in my application
Page 1 of 1
9 Replies - 763 Views - Last Post: 27 October 2010 - 11:06 AM
Replies To: Keyboard shortcuts
#2
Re: Keyboard shortcuts
Posted 27 October 2010 - 12:31 AM
and your problem is ???
what are you trying to do?
also check if your keypreview propertie is set to true
what are you trying to do?
also check if your keypreview propertie is set to true
This post has been edited by NoBrain: 27 October 2010 - 12:32 AM
#3
Re: Keyboard shortcuts
Posted 27 October 2010 - 01:09 AM
Make sure your code is properly typed up, you may just have quickly wrote it here but if you write your code in visual studio with such disregard to case then it wont work.
Do you get any errors? What happens? Have you debugged it? We may seem it, but we're actually not psychic and do require information to give you answers
if(e.KeyCode == Keys.Up)
{
// code here
}
Do you get any errors? What happens? Have you debugged it? We may seem it, but we're actually not psychic and do require information to give you answers
#4
Re: Keyboard shortcuts
Posted 27 October 2010 - 06:46 AM
With only 4 lines of code to go on there isn't much we know about your code.
But my first guess is that whichever control these 4 lines are in, isn't the control that has focus.
If this code is in the form... but a textbox has focus... then the keystrokes are never received by the form's code.
But my first guess is that whichever control these 4 lines are in, isn't the control that has focus.
If this code is in the form... but a textbox has focus... then the keystrokes are never received by the form's code.
#5
Re: Keyboard shortcuts
Posted 27 October 2010 - 10:06 AM
Imdsm, on 27 October 2010 - 12:09 AM, said:
Make sure your code is properly typed up, you may just have quickly wrote it here but if you write your code in visual studio with such disregard to case then it wont work.
Do you get any errors? What happens? Have you debugged it? We may seem it, but we're actually not psychic and do require information to give you answers
if(e.KeyCode == Keys.Up)
{
// code here
}
Do you get any errors? What happens? Have you debugged it? We may seem it, but we're actually not psychic and do require information to give you answers
Hey common buddy I'm not showing any disregard..I can write the code which I'm using here...
my piece of code is
else if (e.KeyCode == Keys.F2)
{
SearchBox.Visible = true;
}
else if (e.KeyCode == Keys.F3)
{
SearchBox.Hide();
}
else if (e.KeyCode == Keys.Up)
{
ScrollBars= scroll();
}
this is what other keys are doing I want the UP keys to move up in the textbox I donot want to use the mouse functionality in my application that's why I'm asking for help from expreienced members..
NoBrain, on 26 October 2010 - 11:31 PM, said:
and your problem is ???
what are you trying to do?
also check if your keypreview propertie is set to true
what are you trying to do?
also check if your keypreview propertie is set to true
Hi,
I'm developing an application where I want to use only keyboard to perform all the operations so I have assigned few keys like
else if (e.KeyCode == Keys.F2)
{
SearchBox.Visible = true;
}
else if (e.KeyCode == Keys.F3)
{
SearchBox.Hide();
}
else if (e.KeyCode == Keys.Up)
{
ScrollBars= scroll();
}
I'm not able to assign scroll option to the UP and DOWN arrow keys.. I have set the KEY PREVIEW to true... But I'm not able to assign functionality to the UP and DOWN arrows..So any help is appreciated...
MOD EDIT: When posting code...USE CODE TAGS!!!
This post has been edited by JackOfAllTrades: 27 October 2010 - 10:45 AM
#6
Re: Keyboard shortcuts
Posted 27 October 2010 - 10:27 AM
all i can think of is this:
maybe it will give you some idea
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
//'a button at the top of the form'
this.ScrollToControl(button1);
break;
case Keys.Down:
//'button at the bottom of the form'
this.ScrollToControl(button2);
break;
}
}
maybe it will give you some idea
#7
Re: Keyboard shortcuts
Posted 27 October 2010 - 10:37 AM
Also, instead of a series of if...else... else if... else if... else if...
May I suggest using a switch construct instead?
#8
Re: Keyboard shortcuts
Posted 27 October 2010 - 10:45 AM
One thing I'd suggest is to isolate the cause of the failure. You're expecting to see something scroll, but what if the scroll method itself is not working?
Try it with a MessageBox telling you when you've pushed down/up. If that works, then you know you're capturing the key events. If not, then at least you've narrowed down your failure points.
Try it with a MessageBox telling you when you've pushed down/up. If that works, then you know you're capturing the key events. If not, then at least you've narrowed down your failure points.
#9
Re: Keyboard shortcuts
Posted 27 October 2010 - 10:58 AM
NoBrain, on 27 October 2010 - 09:27 AM, said:
all i can think of is this:
maybe it will give you some idea
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
//'a button at the top of the form'
this.ScrollToControl(button1);
break;
case Keys.Down:
//'button at the bottom of the form'
this.ScrollToControl(button2);
break;
}
}
maybe it will give you some idea
Hi,
Thanks for your reply, I will sort it out. But I have got one more small issue in the same application on the textbox if an user press CTRL + A then on the terminal it has to display it as <A> how this can be acheieved??
insertAlias, on 27 October 2010 - 09:45 AM, said:
One thing I'd suggest is to isolate the cause of the failure. You're expecting to see something scroll, but what if the scroll method itself is not working?
Try it with a MessageBox telling you when you've pushed down/up. If that works, then you know you're capturing the key events. If not, then at least you've narrowed down your failure points.
Try it with a MessageBox telling you when you've pushed down/up. If that works, then you know you're capturing the key events. If not, then at least you've narrowed down your failure points.
Hi,
That works fine I tried to display a messagebox it works fine, but I'm not sure how to set it up to scroll. Your help is appreciated..
#10
Re: Keyboard shortcuts
Posted 27 October 2010 - 11:06 AM
Dude, you really need to do some experiementing and just plain looking around on your own.
As soon as you type Keys. intellisense pops up a list of all the enumerations etc.
May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your look book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books.
Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.
C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.
Microsoft Visual Studio Tip, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.
The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables
Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio
Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.
As soon as you type Keys. intellisense pops up a list of all the enumerations etc.
private void form_KeyDown(object sender, KeyEventArgs e)
{
if (Form.ActiveForm == null) return; // Ignore keystrokes if we aren't the active application
Keys mods = Control.ModifierKeys; // Just to shorten up the comparrison below
if (mods == Keys.ShiftKey || mods == Keys.Shift || mods == Keys.ControlKey || mods == Keys.Control || mods == Keys.Alt)
return; // We don't respond to system-wide shortcuts like ctrl+shift+4 as if it was an unmodified 4
Console.WriteLine("Keypress ===========================================> " + e.KeyData.ToString());
switch (e.KeyCode)
{
#region Navigation
case Keys.Home:
break;
case Keys.End:
break;
case Keys.PageUp:
break;
case Keys.PageDown:
break;
#endregion
#region Function keys
case Keys.F1:
break;
case Keys.F2:
break;
case Keys.F3:
break;
case Keys.F4:
break;
case Keys.F5:
break;
case Keys.F6:
break;
case Keys.F7:
break;
case Keys.F8:
break;
case Keys.F9:
break;
case Keys.F10:
break;
case Keys.F11:
break;
case Keys.F12:
break;
#endregion
#region Math symbols *+-/
case Keys.Subtract:
break;
case Keys.Decimal:
break;
case Keys.Divide:
break;
case Keys.Multiply:
break;
case Keys.Add:
break;
#endregion
#region Numbers
case Keys.D0:
case Keys.NumPad0:
break;
case Keys.D1:
case Keys.NumPad1:
break;
case Keys.D2:
case Keys.NumPad2:
break;
case Keys.D3:
case Keys.NumPad3:
break;
case Keys.D4:
case Keys.NumPad4:
break;
case Keys.D5:
case Keys.NumPad5:
break;
case Keys.D6:
case Keys.NumPad6:
break;
case Keys.D7:
case Keys.NumPad7:
break;
case Keys.D8:
case Keys.NumPad8:
break;
case Keys.D9:
case Keys.NumPad9:
break;
#endregion
#region Alpha
case Keys.Q:
break;
case Keys.W:
break;
case Keys.E:
break;
case Keys.R:
break;
case Keys.T:
break;
case Keys.Y:
break;
case Keys.U:
break;
case Keys.I:
break;
case Keys.O:
break;
case Keys.P:
break;
case Keys.A:
break;
case Keys.S:
break;
case Keys.D:
break;
case Keys.F:
break;
case Keys.G:
break;
case Keys.H:
break;
case Keys.J:
break;
case Keys.K:
break;
case Keys.L:
break;
case Keys.Z:
break;
case Keys.X:
break;
case Keys.C:
break;
case Keys.V:
break;
case Keys.B:
break;
case Keys.N:
break;
case Keys.M:
break;
case Keys.Space:
break;
#endregion
#region Special purpose
case Keys.Snapshot:
break;
case Keys.Insert:
break;
case Keys.Delete:
break;
case Keys.Back:
break;
case Keys.Tab:
break;
case Keys.Pause:
break;
case Keys.Escape:
break;
case Keys.Separator:
break;
#endregion
case Keys.Enter:
break;
default:
break;
}
}
May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your look book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books.
Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.
C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.
Microsoft Visual Studio Tip, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.
The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables
Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio
Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.
- Anonymous Delegates: Demonstrates the use of unnamed delegates to reduce application complexity.
- Arrays: Shows how to use arrays.
- Attributes: Shows how to create custom attribute classes, use them in code, and query them through reflection.
- Collection Classes: Shows how to make non-generic collection classes that can be used with the foreach statement.
- COM Interop Part I: Shows how to use C# to interoperate with COM objects.
- COM Interop Part II: Shows how to a use a C# server together with a C++ COM client.
- Commandline: Demonstrates simple command-line processing and array indexing.
- Condiational Methods: Demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a symbol is defined.
- Delegates: Shows how delegates are declared, mapped to static and instance methods, and combined into multicast delegates.
- Events: Shows how to declare, invoke, and configure events in C#.
- Explicit Interface: Demonstrates how to explicitly implement interface members and how to access those members from interface instances.
- Generics: Shows how to make generic collection classes that can be used with the foreach statement.
- Hello World: A Hello World application.
- Indexers Part I: Shows how C# classes can declare indexers to provide array-like access to objects.
- Indexers Part II: Shows how to implement a class that uses indexed properties. Indexed properties enable you to use a class that represents an array-like collection.
- Libraries: Shows how to use compiler options to create a DLL from multiple source files; also, how to use the library in other programs
- Named and Optional (C# 4.0): Demonstrates Named and Optional parameters, an alternative to method overloads
- Nullable: Demonstrates value types, such as double and bool, that can be set to null
- Office Sample (C# 4.0): Demonstrates how Dynamic and COM Interop make it easy to call Microsoft Office in C# 4.0
- OLEDB: Demonstrates how to use a Microsoft Access database from C# by creating a dataset and adding tables to it.
- Operator Overloading: Shows how user-defined classes can overload operators
- Partial Types: Demonstrates how classes and structures can be defined in multiple C# source-code files
- PInvoke: Shows how to call exported DLL functions from C#
- Properties: Shows how properties are declared and used; also demonstrates abstract properties
- Python Sample (C# 4.0): Learn how to call a Python script by using the Dynamic feature in C# 4.0
- Security: Discusses .NET Framework security and shows how to modify security permissions in C# by using permission classes and permission attributes
- Simple Variance (C# 4.0): See how Covariance and Contravariance are supported in generic interfaces and delegates
- Structs: Shows how to use structs in C#.
- Threading: Demonstrates various thread activities such as creating and executing a thread, synchronizing threads, interacting between threads, and using a thread pool
- Unsafe: Shows how to use unmanaged code (code that uses pointers) in C#
- User Conversions: Shows how to define conversions to and from user-defined types
- Versioning: Demonstrates versioning in C# by using the override and new keywords
- XML Documents: Shows how to document code by using XML
- Yield: Demonstrates how to use the yield keyword to filter items in a collection
This post has been edited by tlhIn'toq: 27 October 2010 - 11:07 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|