Results 1 to 17 of 17

Thread: get keys on a console app?! how?!

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    get keys on a console app?! how?!

    Well is it possible to recieve the key down event in a console?
    right now I'm doing console.readline which requires Enter to be pressed, I want the program to recieve each key as they are being pressed. is it possible to do with a console app?

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    mm no dont think so..at least nothing straight forward. Console is fairly limited. Perhaps some API's to detect actions in another window.............
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    I saw that there was a method for setting the console input to a TextStream object, though I havn't messed with that yet.

    I'll go see if doing so would allow me to read individual keystrokes.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    there is also some class called
    System.IO._ConsoleStream

    Ill research into that some


    Hu Flung Dung...LOL...thats pretty funny.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ok..you cant do it for sure without api, but i do have a dll I wrote in c# that will do this for you...you want?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    unzip this dll into your projects folder and add a refernce to it. Here is some sample VB code to use it. BTW the reason I put it into a dll is because the code is C# and there were a couple operators I couldnt figure out the VB equivilant of to transfer the code to VB

    Code:
    Imports System
    
    Public Module Test
    	Public Sub Main()
    		Dim tst As New ConsoleMode()
    		tst.EnableCharMode()
    		
    		Dim i As integer
    		While True
    		    i = Console.Read()
    		    Console.Writeline(Microsoft.VisualBasic.Chr(i))
    		    If i = 120 then Exit While ' If x is hit, then exit
    		End While
        		tst.EnableLineMode()
    	End Sub
    End Module
    Attached Files Attached Files
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    Originally posted by Cander
    unzip this dll into your projects folder and add a refernce to it. Here is some sample VB code to use it. BTW the reason I put it into a dll is because the code is C# and there were a couple operators I couldnt figure out the VB equivilant of to transfer the code to VB

    Code:
    Imports System
    
    Public Module Test
    	Public Sub Main()
    		Dim tst As New ConsoleMode()
    		tst.EnableCharMode()
    		
    		Dim i As integer
    		While True
    		    i = Console.Read()
    		    Console.Writeline(Microsoft.VisualBasic.Chr(i))
    		    If i = 120 then Exit While ' If x is hit, then exit
    		End While
        		tst.EnableLineMode()
    	End Sub
    End Module
    You didn't need to compile it. You could have just uploaded the CS file (is that the C# code file extension?). Then we could have added it to our projects. Just because you have a Visual Basic project doesn't mean that all of your code needs to be in Visual Basic. You could have 1 file written in VB, another in C#, and another in C++, and they would all compile together just fine.

    Ahh, gotta love .NET!

    Anyway, I was busy just now so I couldn't check out the TextStream object. I'll do that right now.

    EDIT:

    Never mind. Mis-understood your post.
    Last edited by Hu Flung Dung; Aug 7th, 2002 at 02:17 PM.

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yeah , but I dont use Visual Studio, just the command line and notepad so I dont get that luxury..

    Ill eventually isntall VS .Net but right now I have been quite happy with notepad



    but here is the actual class code if you want it

    Code:
    using System;
    using System.Runtime.InteropServices;
    
    public class ConsoleMode
    {
    
    	const int STD_INPUT_HANDLE = -10;
    	const int ENABLE_LINE_INPUT = 0x0002;
    	const int ENABLE_ECHO_INPUT = 0x0004;
    	[DllImport("kernel32")]
    	public static extern IntPtr GetStdHandle(int nStdHandle);
    	[DllImport("kernel32")]
    	public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
    	[DllImport("kernel32")]
    	public static extern bool GetConsoleMode(IntPtr hConsoleHandle, ref int pmode);
    
    	
    	public static void EnableCharMode()
    	{
    		IntPtr hConsole;
    		int mode= 0;
    		hConsole = GetStdHandle(STD_INPUT_HANDLE);
    		GetConsoleMode(hConsole, ref mode);
    		mode &= ~ (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
    		SetConsoleMode(hConsole, mode);
    	}
    
    	public static void EnableLineMode()
    	{
    		IntPtr hConsole;
    		int mode=0;
    		hConsole = GetStdHandle(STD_INPUT_HANDLE);
    		GetConsoleMode(hConsole, ref mode);
    		mode |= (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
    		SetConsoleMode(hConsole, mode);
    	}
    }
    btw if anyone knows the equvialnt of what ~ and |= is for VB, please let me know
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    I'm not completely sure what those do. Seems like their bitwise operators (the bitwise operators in VB are AND, OR, XOR, and NOT) from what I see in MSDN, but it doesn't describe their functionality too well.

    Anyway, you should really install Visual Studio .NET. Its over 2 GB (pretty large, heh) but its definately worth it. You get MSDN help, intellisense, tons of debugging tools, you can easily design forms, it has a web-page editor built in (for ASP.NET), and a whole bunch of other impressive features.

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yeah i think |= might remove values bitwise. It is obviously the opposite of &= but VB .NET supports &= but apparently not |=

    I will eventually install VS .NET, I just feel I am learning alot more by doing everything in notepad. Plus I have a new computer coming in for me at work in a couple months, but the one I have here now just wont handle VS .NET to well.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    stupid me..why didnt i just look in my C# book,

    ~ isnt listed but
    | is the Or bitwise operator.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  12. #12
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ~ is a binary inverter basically it inverts the binary value of a byte

    now if I can just figure out the VB equivilant.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  13. #13

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    eeeeeh Cander, you have an answer for every question, dont you?

    Thanks a lot for your help... I added a reference to the dll file but for some reason it doesnt work. It says that consoleMode isnt defined (Type 'ConsoleMode' is not defined.)

    btw MS did a good favor to everyone by adding console apps to VS so you dotn have to mess with APIs. Now why the hell didnt they make it somehow that it would work compeletly?!!!!

  14. #14
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    well the console is pretty much useless in windows anyway

    umm..

    change ConsoleMode to Con as I may have kept it as Con when I compiled the dll
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  15. #15

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    WORKS!!! thanks

  16. #16
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Red face

    Cander is awsome. he should fix my tiny problem for me. heh.
    Magiaus

    If I helped give me some points.

  17. #17
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Originally posted by Magiaus
    Cander is awsome. he should fix my tiny problem for me. heh.
    man my ego is growing!

    Read the reply to your post..
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width