473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Catching the ALT key pressed?

Hello. I am trying to catch the ALT key (but not as a modifier, but as a
single key clicked) on a Console application, but neither Read(),
ReadLine() nor ReadKey() catch it, how can I do it using the
System.Console class?

Thanks in advance.

Regards.

Andrés [ knocte ]

--
Sep 11 '06 #1
4 7900
This isn't possible in .NET as far as I'm aware, you will need to hook into
the Windows API to handle the keypress events at a far lower level as .NET
treats the ALT key as a modifier only.

There is an article about hooking in but for a different reason here (but it
should be adaptable):
http://www.codeproject.com/useritems/CSLLKeyboard.asp

Ciaran O'Donnell

""Andrés G. Aragoneses [ knocte ]"" wrote:
Hello. I am trying to catch the ALT key (but not as a modifier, but as a
single key clicked) on a Console application, but neither Read(),
ReadLine() nor ReadKey() catch it, how can I do it using the
System.Console class?

Thanks in advance.

Regards.

Andrés [ knocte ]

--
Sep 11 '06 #2
It can be obtained by overriding the ProcessCommandKey method. Here's an
example (note that I created a separate class to hold WinAPI constants):

/// <summary>
/// KeyDown Event handler which handles control keys as well as regular
/// </summary>
/// <param name="msg"><c>System.Windows.Forms.Message</c></param>
/// <param name="keyData">Key Data</param>
/// <returns>True if handled; false if not</returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!toolPageNumber.Focused && !toolDocumentName.Focused && msg.Msg ==
WinUserApi.WM_KEYDOWN)
{
switch ((int)msg.WParam)
{
case WinUserApi.VK_UP:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (vertScroll.Visible)
ScrollVertical(Math.Max(vertScroll.Value - vertScroll.SmallChange,
0));
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(0, -1);
}
break;
case WinUserApi.VK_DOWN:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (vertScroll.Visible)
ScrollVertical(Math.Min(vertScroll.Value + vertScroll.SmallChange,
_MaxNegativeVerticalSpace));
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(0, 1);
}
break;
case WinUserApi.VK_LEFT:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (horizScroll.Visible)
ScrollHorizontal(horizScroll.Value - horizScroll.SmallChange);
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(-1, 0);
}
break;
case WinUserApi.VK_RIGHT:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (horizScroll.Visible)
ScrollHorizontal(horizScroll.Value + horizScroll.SmallChange);
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(1, 0);
}
break;
case WinUserApi.VK_PRIOR:
if (!InputEnabled) return true;
if (pagePanel.SelectedElements.Count == 0)
{
if (vertScroll.Visible)
ScrollVertical(Math.Max(vertScroll.Value - vertScroll.LargeChange,
0));
return true;
}
else
pagePanel.MoveCurrentPrintElementBy(0, -20);
break;
case WinUserApi.VK_NEXT:
if (!InputEnabled) return true;
if (pagePanel.SelectedElements.Count == 0)
{
if (vertScroll.Visible)
ScrollVertical(Math.Min(vertScroll.Value + vertScroll.LargeChange,
_MaxNegativeVerticalSpace));
return true;
}
else
pagePanel.MoveCurrentPrintElementBy(0, 20);
break;
case WinUserApi.VK_DELETE:
if (!InputEnabled) return true;
pagePanel.DeleteCurrentElement();
return true;
case WinUserApi.VK_CONTROL:
pagePanel.HandleElementMouseEvents = false;
break;
case WinUserApi.VK_TAB:
pagePanel.HandleElementMouseEvents = false;
if (!InputEnabled || pagePanel.HandleElementMouseEvents) return true;
if (pagePanel.PrintElements.HighestZIndex == -1) return true;
int i = pagePanel.PrintElements.IndexOfZ(_CurrentZIndex);
if (i == -1) i = 0;
if (pagePanel.ShiftOn) i = (i == 0 ?
pagePanel.PrintElements.HighestZIndex : i - 1);
else i = (i == pagePanel.PrintElements.HighestZIndex ? 0 : i + 1);
if (i == -1) return false;
pagePanel.CurrentPrintElement = pagePanel.PrintElements[i];
if (pagePanel.CurrentPrintElement != null)
{
pagePanel.CurrentPrintElement.ShowToolTip();
ShowStatusMessage("PrintElements[" + i.ToString() +
"] (" + pagePanel.PrintElements[i].ToString() +
") " + pagePanel.CurrentPrintElement.Location.ToString()) ;
}
return true;
case WinUserApi.VK_SHIFT:
pagePanel.ShiftOn = true;
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}

"Ciaran O''Donnell" <Ci************@discussions.microsoft.comwrote in
message news:FC**********************************@microsof t.com...
This isn't possible in .NET as far as I'm aware, you will need to hook
into
the Windows API to handle the keypress events at a far lower level as .NET
treats the ALT key as a modifier only.

There is an article about hooking in but for a different reason here (but
it
should be adaptable):
http://www.codeproject.com/useritems/CSLLKeyboard.asp

Ciaran O'Donnell

""Andrés G. Aragoneses [ knocte ]"" wrote:
>Hello. I am trying to catch the ALT key (but not as a modifier, but as a
single key clicked) on a Console application, but neither Read(),
ReadLine() nor ReadKey() catch it, how can I do it using the
System.Console class?

Thanks in advance.

Regards.

Andrés [ knocte ]

--

Sep 11 '06 #3
Kevin Spencer escribió:
It can be obtained by overriding the ProcessCommandKey method. Here's an
example (note that I created a separate class to hold WinAPI constants):
Thanks for the info, but this code snippet seems only valid for a
WinForms application (instead of a Console application) and it seems
very tied to Win32 API (so I suppose for Linux with Mono it won't work
and it would be more code needed), am I right?

Regards,

Andrés [ knocte ]

--
Sep 12 '06 #4
Kevin,
Can it be used to catch teh Send and End Keys also.
I am trying to capture the End(Red) and Send(Green) keys for my call
application .How can i do that in C#

"Kevin Spencer" wrote:
It can be obtained by overriding the ProcessCommandKey method. Here's an
example (note that I created a separate class to hold WinAPI constants):

/// <summary>
/// KeyDown Event handler which handles control keys as well as regular
/// </summary>
/// <param name="msg"><c>System.Windows.Forms.Message</c></param>
/// <param name="keyData">Key Data</param>
/// <returns>True if handled; false if not</returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!toolPageNumber.Focused && !toolDocumentName.Focused && msg.Msg ==
WinUserApi.WM_KEYDOWN)
{
switch ((int)msg.WParam)
{
case WinUserApi.VK_UP:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (vertScroll.Visible)
ScrollVertical(Math.Max(vertScroll.Value - vertScroll.SmallChange,
0));
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(0, -1);
}
break;
case WinUserApi.VK_DOWN:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (vertScroll.Visible)
ScrollVertical(Math.Min(vertScroll.Value + vertScroll.SmallChange,
_MaxNegativeVerticalSpace));
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(0, 1);
}
break;
case WinUserApi.VK_LEFT:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (horizScroll.Visible)
ScrollHorizontal(horizScroll.Value - horizScroll.SmallChange);
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(-1, 0);
}
break;
case WinUserApi.VK_RIGHT:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (horizScroll.Visible)
ScrollHorizontal(horizScroll.Value + horizScroll.SmallChange);
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(1, 0);
}
break;
case WinUserApi.VK_PRIOR:
if (!InputEnabled) return true;
if (pagePanel.SelectedElements.Count == 0)
{
if (vertScroll.Visible)
ScrollVertical(Math.Max(vertScroll.Value - vertScroll.LargeChange,
0));
return true;
}
else
pagePanel.MoveCurrentPrintElementBy(0, -20);
break;
case WinUserApi.VK_NEXT:
if (!InputEnabled) return true;
if (pagePanel.SelectedElements.Count == 0)
{
if (vertScroll.Visible)
ScrollVertical(Math.Min(vertScroll.Value + vertScroll.LargeChange,
_MaxNegativeVerticalSpace));
return true;
}
else
pagePanel.MoveCurrentPrintElementBy(0, 20);
break;
case WinUserApi.VK_DELETE:
if (!InputEnabled) return true;
pagePanel.DeleteCurrentElement();
return true;
case WinUserApi.VK_CONTROL:
pagePanel.HandleElementMouseEvents = false;
break;
case WinUserApi.VK_TAB:
pagePanel.HandleElementMouseEvents = false;
if (!InputEnabled || pagePanel.HandleElementMouseEvents) return true;
if (pagePanel.PrintElements.HighestZIndex == -1) return true;
int i = pagePanel.PrintElements.IndexOfZ(_CurrentZIndex);
if (i == -1) i = 0;
if (pagePanel.ShiftOn) i = (i == 0 ?
pagePanel.PrintElements.HighestZIndex : i - 1);
else i = (i == pagePanel.PrintElements.HighestZIndex ? 0 : i + 1);
if (i == -1) return false;
pagePanel.CurrentPrintElement = pagePanel.PrintElements[i];
if (pagePanel.CurrentPrintElement != null)
{
pagePanel.CurrentPrintElement.ShowToolTip();
ShowStatusMessage("PrintElements[" + i.ToString() +
"] (" + pagePanel.PrintElements[i].ToString() +
") " + pagePanel.CurrentPrintElement.Location.ToString()) ;
}
return true;
case WinUserApi.VK_SHIFT:
pagePanel.ShiftOn = true;
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}

"Ciaran O''Donnell" <Ci************@discussions.microsoft.comwrote in
message news:FC**********************************@microsof t.com...
This isn't possible in .NET as far as I'm aware, you will need to hook
into
the Windows API to handle the keypress events at a far lower level as .NET
treats the ALT key as a modifier only.

There is an article about hooking in but for a different reason here (but
it
should be adaptable):
http://www.codeproject.com/useritems/CSLLKeyboard.asp

Ciaran O'Donnell

""Andrés G. Aragoneses [ knocte ]"" wrote:
Hello. I am trying to catch the ALT key (but not as a modifier, but as a
single key clicked) on a Console application, but neither Read(),
ReadLine() nor ReadKey() catch it, how can I do it using the
System.Console class?

Thanks in advance.

Regards.

Andrés [ knocte ]

--


Oct 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olli Piepponen | last post by:
Hi, I'm having a little problem catching keystrokes under Windows. I did a little research and found that with mscvrt.getch() one can cath a single key that is pressed. However this doesn't work...
0
by: Dell Stinnett | last post by:
How do I go about checking user input to see if the user has pressed F1? I need to be able to redirect F1 to open a .pdf file to provide help for the specific module the user is in - I can get the...
1
by: Nagachandra Sekhar Grandhi | last post by:
I placed a combobox control on User control and this user control was placed on a form and I tried to catch the keys pressed on that combo box, but I am unable to do it. I set the property of...
0
by: Michael Howes | last post by:
I'm trying to handle a number of keystrokes in a TextBox and for other reasons I'm using the KeyDown I'm trying to know when Shift-Tab is pressed and the following code doesn't work if ( (...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
9
by: OpticTygre | last post by:
How would I be able to capture multiple key presses, such as Alt+Shift+D in a form's KeyPress and KeyDown events? Thanks in advance. -Jason
7
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
2
by: Cruelemort | last post by:
Hello all, I am new to this group (and new to Python) and was hoping someone would be able to help me with something, it is not so much a problem it is more of a general interest query about...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.