473,320 Members | 1,862 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,320 software developers and data experts.

console app problem

Hi...

I'm trying to find some code samples that will show me how to capture
non-character keypresses in a console app. I've tried the standard stuff,
such as console.read and console.readline, and I've tried ConsoleEx which
does a great job, but still doesn't handle non-character keys like CTRL and
the arrow keys.

I've read the character mode apps page on MSDN, and couldn't really make
much sense out of it.

So I guess my question is, has ANYONE successfully trapped arrow keys in a
console app, using C#, C++.net, or VB.net? I would really reaaaaally
appreciate any help I can get, thanks.

Finally, if I can't do it, I'll need to write it as a windows app... so the
question is, how do I write to the screen of a windows app, as if it were a
console? (I've seen it done, I just don't know how.)

Thanks,

Chris

--
development journal: http://www.mystictriad.com/dev
Heroic Adventure 0.1.1 (HA! for short)
Probably the first Roguelike written in VB.NET
Nov 13 '05 #1
4 3781
In article <e$**************@TK2MSFTNGP10.phx.gbl>,
sp**@spamspamspamspam.com says...
Hi...

I'm trying to find some code samples that will show me how to capture
non-character keypresses in a console app. I've tried the standard stuff,
such as console.read and console.readline, and I've tried ConsoleEx which
does a great job, but still doesn't handle non-character keys like CTRL and
the arrow keys.

I've read the character mode apps page on MSDN, and couldn't really make
much sense out of it.

So I guess my question is, has ANYONE successfully trapped arrow keys in a
console app, using C#, C++.net, or VB.net? I would really reaaaaally
appreciate any help I can get, thanks.


Maybe if you put the console into character mode instead of line mode
you could capture the keystrokes you're looking for. See this post:

http://tinyurl.com/h6nm

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 13 '05 #2
> Finally, if I can't do it, I'll need to write it as a windows app... so
the
question is, how do I write to the screen of a windows app, as if it were a console? (I've seen it done, I just don't know how.)
Do I understand this correctly, your trying to make a Windows App
behave/emulate a Console App?

~V
"Chris Williams" <sp**@spamspamspamspam.com> wrote in message
news:e$**************@TK2MSFTNGP10.phx.gbl... Hi...

I'm trying to find some code samples that will show me how to capture
non-character keypresses in a console app. I've tried the standard stuff,
such as console.read and console.readline, and I've tried ConsoleEx which
does a great job, but still doesn't handle non-character keys like CTRL and the arrow keys.

I've read the character mode apps page on MSDN, and couldn't really make
much sense out of it.

So I guess my question is, has ANYONE successfully trapped arrow keys in a
console app, using C#, C++.net, or VB.net? I would really reaaaaally
appreciate any help I can get, thanks.

Finally, if I can't do it, I'll need to write it as a windows app... so the question is, how do I write to the screen of a windows app, as if it were a console? (I've seen it done, I just don't know how.)

Thanks,

Chris

--
development journal: http://www.mystictriad.com/dev
Heroic Adventure 0.1.1 (HA! for short)
Probably the first Roguelike written in VB.NET

Nov 13 '05 #3
character mode does allow me to trap character keys, but I still cant get
the non-character keys like Ctrl and Shift and the arrow keys, etc...

Thanks though,

S.

--
development journal: http://www.mystictriad.com/dev
Heroic Adventure 0.1.1 (HA! for short)
Probably the first Roguelike written in VB.NET
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <e$**************@TK2MSFTNGP10.phx.gbl>,
sp**@spamspamspamspam.com says...
Hi...

I'm trying to find some code samples that will show me how to capture
non-character keypresses in a console app. I've tried the standard stuff, such as console.read and console.readline, and I've tried ConsoleEx which does a great job, but still doesn't handle non-character keys like CTRL and the arrow keys.

I've read the character mode apps page on MSDN, and couldn't really make
much sense out of it.

So I guess my question is, has ANYONE successfully trapped arrow keys in a console app, using C#, C++.net, or VB.net? I would really reaaaaally
appreciate any help I can get, thanks.


Maybe if you put the console into character mode instead of line mode
you could capture the keystrokes you're looking for. See this post:

http://tinyurl.com/h6nm

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele

Nov 13 '05 #4
"Chris Williams" <sp**@spamspamspamspam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
nope, I'm trying to write a console app that captures non-character
keypresses as part of the program.


You need to use native methods to get that level of control. The following
code demonstrates
getting the console's input handle, waiting on an input events and echoing
the scan-code to the
debug output.

static void Main(string[] args){
uint nRead = 0;
INPUT_RECORD iRecord = new INPUT_RECORD();
IntPtr stdIn = GetStdHandle(STD_INPUT_HANDLE);
AutoResetEvent are = new AutoResetEvent(false);
are.Handle = stdIn;
for(;;){
are.WaitOne();
System.Diagnostics.Debug.WriteLine("released");
int ret = ReadConsoleInput(stdIn, ref iRecord, 1, ref nRead);
for(int n=0; n!=nRead; ++n)
if(iRecord.EventType==KEY_EVENT){
ushort vk = iRecord.wVirtualScanCode;
if(vk==0x2A) System.Diagnostics.Debug.WriteLine("Left Shift");
else
System.Diagnostics.Debug.WriteLine(vk.ToString());
}
}
}
const uint STD_INPUT_HANDLE = 0xFFFFFFFF-9;
const ushort KEY_EVENT = 0x0001;
[DllImport("Kernel32")]
public static extern IntPtr GetStdHandle(uint nStdHandle);
[DllImport("Kernel32")]
public static extern int ReadConsoleInput(IntPtr hConsoleInput, ref
INPUT_RECORD lpBuffer, uint nLength, ref uint lpNumberOfEventsRead);
[StructLayout(LayoutKind.Sequential)]
public struct INPUT_RECORD
{
public ushort EventType;
public uint bKeyDown;
public ushort wRepeatCount;
public ushort wVirtualKeyCode;
public ushort wVirtualScanCode;
public char UnicodeChar;
public uint dwControlKeyState;
}
Nov 15 '05 #5

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

Similar topics

1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
2
by: glean | last post by:
My console in WinXP is using Lucida Console for the font. My problem is, I can go to character map and select a string of special characters such as the following: ®¾Öþ¬«§¯ I can paste them into...
7
by: shawnk | last post by:
Hello Everyone How do you format format numbers right-justified using Console.WriteLine(), i.e I need to line up numbers in vertical columns and the MSDN documentation is pretty poor Here is the...
11
by: objectref | last post by:
Hi to all, i have a window app and i want to display some info in a console window. I figured out (after a very long search...) how am i supposed to do it and i try using the following code. ...
5
by: Publicjoe | last post by:
I am working on a little app which uses colour in the console window. I have created a class to extend the console functionality but the ClearScreen method does not work correctly. I am enclosing a...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
6
by: Giojo | last post by:
Hello guys! I can't resolve this problem.. I want my programm in c# working with only console if there are some parameters, but if someone make double-click on the exe I want to start the graphic...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.