By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

Catching the ALT key pressed?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.