Help! reset keyboard?

Dec 20, 2010 at 2:44 AM
Edited Dec 20, 2010 at 11:30 AM

Sorry for my bad english.

Hello im making a basic OS whit a game collection.I whant to include the snake demo (not the vga one) so i replaced the last line of code (the reboot one) to get back to the shell.But,it seem that the keyboard is disabled somehow and i noticed this line of code :

 Cosmos.Hardware.Keyboard.Initialize(KeyPress); 

So,it seem i need to "reset" the keyboard?I tryed this whit no sucses:

 Cosmos.Hardware.Keyboard.Initialize();

Help plz.Thanks in advance.

 

Developer
Dec 20, 2010 at 1:42 PM

Have u tried Cosmos.Hardware.Keyboard.ReadKey().

Dec 20, 2010 at 9:01 PM

Just tryed it but it imediately hang...

Developer
Dec 20, 2010 at 9:52 PM
Edited Dec 20, 2010 at 9:53 PM

If you are using an OS Shell just call the part of your program that gets user input. If you have a gui clear the screen,redraw it with everything and enter your gui loop.

Or if you want post your  OS SHELL loop if your os is a shell or your OS GUI  loop if your os is a GUI here.

Dec 20, 2010 at 11:50 PM
Edited Dec 21, 2010 at 8:55 PM

So,here it is.To be honest i have no idea why it dont work.

So,as you can see,after finishing the "game" , it just goto the label NEXTAR(dunno why i named it like that...) and it take back the loop...It should work.If i add Keyboard.ReadKey(); it hang immediately after it reach it,if i dont toutch anyting it make its way to the Console.Readline(); but then the keyboard dont work...It should work perfect...

 

 

        //CORE

            START: 
            Console.Clear();
            Xenoh_OS.MainShell.msh(); //execute the mainchell (Just a welcome line of text,dunno wy i named it "mainshell"...

            LOOP:
            //mainloop
            
            //input
            Console.Write("Xen:");
            string input1 = Console.ReadLine();


            if (input1 == "shutdown")
            {
                Xenoh_OS.PRlib.shut();
                goto NEXTAR;
            }
            if (input1 == "write")
            {
                Xenoh_OS.PRlib.wrt();
                goto NEXTAR;
            }
            if (input1 == "cls")
            {
                Xenoh_OS.PRlib.cls();
                goto NEXTAR;
            }
            if (input1 == "game")
            {
                Xenoh_OS.PRlib.game();
                goto NEXTAR;
            }
            //menu


            //notfound
            Console.BackgroundColor = ConsoleColor.Red;
            Console.ForegroundColor = ConsoleColor.Yellow;

            if (input1 == "")
            {
                Console.WriteLine("Write someting?");

            }
            else
            {
                Console.WriteLine("Error:Cant find " + input1 + " in PRlib.cs");
            }

            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Gray;

 
        NEXTAR:
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Gray;

            servo.Wait(5); //Litle delay in sec (not real sec..1/4 sec)..

            Console.WriteLine("");
            
            goto LOOP;


            //shutdown
            Cosmos.Sys.Deboot.ShutDown();
            //CORE
Developer
Dec 21, 2010 at 7:12 PM
Edited Dec 21, 2010 at 7:17 PM

Why are you using labels. You know you could use a method right?

use this methods instead of your  goto NEXTAR l function.

 

public static void NEXTAR()

{

Console.BackgroundColor = ConsoleColor.Black;

            Console.ForegroundColor = ConsoleColor.Gray;

            servo.Wait(5); //Litle delay in sec (not real sec..1/4 sec)..

            Console.WriteLine("");   
            MainLoop();//Calls your main loop         

}

public static void MainLoop()

{

            Console.Write("Xen:");
            string input1 = Console.ReadLine();


            if (input1 == "shutdown")
            {
                Xenoh_OS.PRlib.shut();
                NEXTAR();
            }
            else if (input1 == "write")
            {
                Xenoh_OS.PRlib.wrt();
                 NEXTAR();
            }
            else if (input1 == "cls")
            {
                Xenoh_OS.PRlib.cls();
                NEXTAR();
} else if (input1 == "game") { Xenoh_OS.PRlib.game(); NEXTAR(); } else if (input1 == "")
            {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.ForegroundColor = ConsoleColor.Yellow; 
            Console.WriteLine("Write someting?");
             MainLoop();
else  
            {                
              Console.WriteLine("Error:Cant find " + input1 + " in PRlib.cs");
              MainLoop();
            }

  }

 

Since the Snake Game is in a loop. You have to call the method when the snake is killed or Escape is press. I have the same game so i know it has a event for escape. Find that event and change the code from

Cosmos.Sys.Deboot.ShutDown();

to

MainLoop();//Calls your mainloop



Dec 21, 2010 at 9:12 PM

Yeah changing label to metode is in my todo list,i alredy replaced the shutdown so it should get back to nextar and continu normally,i just have the impression the keyboard is frozen....

Dec 22, 2010 at 12:40 AM

Help,any one?

Developer
Dec 22, 2010 at 2:58 AM

Hey the keyboard doesn't freeze. If you started your os my Initializing the keyboard it wouldn't stall.You need to either use Keyboard.GetKey, Keyboard.ReadKey or if using a console Console.ReadLine.

if you are using a loop you need to call the loop's method everytime a instruction is finished processing. Like so

lets say i have a man loop name 'ReadInput'

the method would be;

 

public static void ReadInput()

{

     string UserInput;

      UserInput=Console.ReadLine();

        //u can have a function here to convert the input to lower or upper case.

     ProcessInput();// Call my method to process the users input.

}

 

public static void ProcessInput()

 {

     if(UserInput.Equals("hello"))

      {

         console.WriteLine("HELLO THERE");

         ReadInput();//Finished processing input so call my ReadInput method to get input from keyboard    

      }

     else if(UserInput.StartsWith("echo-"))

      {

         string Temp0;

         Temp0=UserInput.Substring(5);

         console.WriteLine(Temp0);

         ReadInput();//Finished processing input so call my ReadInput method to get input from keyboard

      }

     else if(UserInput.Equals("reboot"))

       {

          Cosmos.Sys.Deboot.Reboot();//This function doesn't call ReadInput because we are turning of the pc.

        }

   

   else if(UserInput.Equals("shutdown"))

       {

          Cosmos.Sys.Deboot.Shutdown();//This function doesn't call ReadInput because we are turning of the pc.

        }

 else

    {

        Console.WriteLine("No Input");

         ReadInput();// Finished processing input so call my ReadInput method to get input from keyboard

     }

}

 

If you are trying to make it work on a gui together with mouse then u just have to put them in the same loop and make a Text Driver to draw the text that is entered to the screen.

Dec 22, 2010 at 11:06 PM

Thats not my issue,in the snake game the code start whit a Cosmos.Hardware.Keyboard.Initialize(KeyPress); , then it seem impossible to "reset" the keyboard for a Console.Readline(); , try it your self :

Cosmos.Hardware.Keyboard.Initialize(KeyPress);

Console.Readline();

My question is the line of code i need to put betwen the two above line in order for the keyboeard to work again ...

 

Developer
Dec 23, 2010 at 10:46 PM
Edited Dec 23, 2010 at 10:47 PM

Do you have a OS Shell or  GUI ? 

Dec 23, 2010 at 11:09 PM

Shell...

Developer
Dec 23, 2010 at 11:35 PM

 post the Snake Game Code here

 

Dec 24, 2010 at 1:52 AM
using System;
using Cosmos.Compiler.Builder;
using Cosmos.Hardware.PC.Bus;
using Cosmos.Hardware;
using System.Diagnostics;
using S = Cosmos.Hardware.TextScreen;

namespace CosmosBoot
{
    class snake
    {

        enum Direction
        {
            Left=0,
            Up,
            Right,
            Down
        }

        enum Key
        {
            Escape = 1,
            Up = 72,
            Left = 75,
            Right = 77,
            Down = 80
        }

        enum Blocked
        {
            Empty = 0,
            Snake,
            Fruit,
        }

        /// <summary>
        /// Object to hold player data
        /// </summary>
        private class Player
        {
            public int X,Y;
            public Direction direction;
            public bool Alive;
        }

        static Player xsnake = null;
        static bool running = true;

        public static void KeyPress(byte aScanCode, bool aReleased)
        {
            if (xsnake != null && !aReleased)
            {
                if (aScanCode == (byte)Key.Left && xsnake.direction != Direction.Right)
                {
                    xsnake.direction = Direction.Left;
                }
                else if (aScanCode == (byte)Key.Up && xsnake.direction != Direction.Down)
                {
                    xsnake.direction = Direction.Up;
                }
                else if (aScanCode == (byte)Key.Right && xsnake.direction != Direction.Left)
                {
                    xsnake.direction = Direction.Right;
                }
                else if (aScanCode == (byte)Key.Down && xsnake.direction != Direction.Up)
                {
                    xsnake.direction = Direction.Down;
                }
                else if (aScanCode == (byte)Key.Escape)
                    running = false;
            }
        }

        private class Random
        {
            private int a = 214013;
            private int x = 0x72535;
            private int c = 2531011;

            public Random(int seed)
            {
                x = seed;
            }
            public int Next(int p)
            {
                x = (a * x + c);
                return x % p;
            }
        }

        static Random rand = null;

        public static void Init()
        {

            S.ReallyClearScreen();
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            S.SetColors(ConsoleColor.Yellow, ConsoleColor.DarkBlue);
            Console.Write("                                                                                ");
            /*
            Console.Write("                                                                                ");
            Console.Write("                  .d8888b.                    888                               ");
            Console.Write("                 d88P  Y88b                   888                               ");
            Console.Write("                 Y88b.                        888                               ");
            Console.Write("                  .Y888b.   88888b.   8888b.  888  888  .d88b.                  ");
            Console.Write("                     .Y88b. 888 .88b     .88b 888 .88P d8P  Y8b                 ");
            Console.Write("                       /888 888  888 .d888888 888888K  88888888                 ");
            Console.Write("                 Y88b  d88P 888  888 888  888 888 /88b Y8b.                     ");
            Console.Write("                  .Y8888P.  888  888 .Y888888 888  888  .Y8888                  ");*/
            Console.Write("                                                                                ");
            Console.Write("                   _____                   _                                    ");
            Console.Write("                  / ____|                 | |                                   ");
            Console.Write("                 | (___    _ __     __ _  | | __   ___                          ");
            Console.Write("                  \\___ \\  | '_ \\   / _` | | |/ /  / _ \\                         ");
            Console.Write("                  ____) | | | | | | (_| | |   <  |  __/                         ");
            Console.Write("                 |_____/  |_| |_|  \\__,_| |_|\\_\\  \\___|                         ");
            Console.Write("                                                                                ");
            Console.Write("                                                                                ");
            S.SetColors(ConsoleColor.White, ConsoleColor.Black);
            Console.WriteLine("");     
            Console.Write("                 Press enter to begin.                                        ");
            Console.ReadLine();

            Cosmos.Hardware.Keyboard.Initialize(KeyPress); //THE PROBLEM LINE
                
            S.SetColors(ConsoleColor.Black, ConsoleColor.Black);

            S.ReallyClearScreen();

            xsnake = new Player()
            {
                X = S.Columns / 2,
                Y = S.Rows / 2,
                direction = Direction.Left,
                Alive = true
            };
            int score = 0;

            Blocked[] isBlocked = new Blocked[S.Columns * S.Rows];

            rand = new Random((int)Cosmos.Hardware.Global.TickCount
                + Cosmos.Hardware.RTC.GetSeconds());

            Player fruit = new Player()
            {
                X=rand.Next(S.Columns),
                Y=rand.Next(S.Rows)
            };

            S.SetColors(ConsoleColor.Red, ConsoleColor.Red);
            S.PutChar(fruit.Y, fruit.X, '*');
            isBlocked[fruit.X + fruit.Y * S.Columns] = Blocked.Fruit;

            int pause = 1;

            int[] xpos = new int[100];
            int[] ypos = new int[100];
            int numpos = 0;
            int deleteat = 0;

            while (xsnake.Alive)
            {
                if (!running)
                    break;

                if (xsnake.direction == Direction.Up || xsnake.direction == Direction.Down)
                    Cosmos.Hardware.PIT.Wait(100);//Going up or down is faster then left or right so slow down
                else
                    Cosmos.Hardware.PIT.Wait(50);

                S.SetColors(ConsoleColor.White, ConsoleColor.Black);
                string strscore = score.ToString();
                for (int i = 0; i < strscore.Length; i++)
                {
                    S.PutChar(0, i + 5, strscore[i]);
                }

                S.SetColors(ConsoleColor.Gray, ConsoleColor.Gray);
                S.PutChar(xsnake.Y, xsnake.X, 'X');
                xpos[numpos] = xsnake.X;
                ypos[numpos] = xsnake.Y;
                numpos++;

                if (pause < 1)
                {
                    if(xpos[deleteat] != -1 && ypos[deleteat] != -1)
                    {
                        S.RemoveChar(ypos[deleteat], xpos[deleteat]);
                        isBlocked[xpos[deleteat] + ypos[deleteat] * S.Columns] = Blocked.Empty;
                        xpos[deleteat] = ypos[deleteat] = -1;
                        deleteat++;
                    }
                }
                else
                    pause--;

                isBlocked[xsnake.X + xsnake.Y * S.Columns] = Blocked.Snake;
                
                switch(xsnake.direction)
                {
                    case Direction.Left:
                        xsnake.X--;
                        break;
                    case Direction.Up:
                        xsnake.Y--;
                        break;
                    case Direction.Right:
                        xsnake.X++;
                        break;
                    case Direction.Down:
                        xsnake.Y++;
                        break;
                }

                if (xsnake.X > S.Columns || xsnake.Y > S.Rows || xsnake.X < 0 || xsnake.Y < 0 
                    || isBlocked[xsnake.X + xsnake.Y * S.Columns] == Blocked.Snake)
                {
                    xsnake.Alive = false;
                }
                else if (isBlocked[xsnake.X + xsnake.Y * S.Columns] == Blocked.Fruit)
                {
                    isBlocked[fruit.X + fruit.Y * S.Columns] = Blocked.Empty;
                    fruit.X = rand.Next(S.Columns);
                    fruit.Y = rand.Next(S.Rows);
                    S.SetColors(ConsoleColor.Red, ConsoleColor.Red);
                    S.PutChar(fruit.Y, fruit.X, '*');
                    isBlocked[fruit.X + fruit.Y * S.Columns] = Blocked.Fruit;
                    pause++;
                    score++;
                }
            }

            
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Clear();
            S.ReallyClearScreen();
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("Shutting down snake game");
            Console.WriteLine("Your score is " + score);
            //Cosmos.Hardware.Keyboard.Initialize();
        }

    }
}

Dec 24, 2010 at 2:18 PM

Any help,my question is so simple,whats the problem?

Coordinator
Dec 24, 2010 at 2:35 PM
The correct way of using the keyboard is using Console.Read/ReadLine/ReadKey, although some may be unimplemented yet


On Fri, Dec 24, 2010 at 4:19 PM, FFA702 <notifications@codeplex.com> wrote:

From: FFA702

Any help,my question is so simple,whats the problem?

Read the full discussion online.

To add a post to this discussion, reply to this email (Cosmos@discussions.codeplex.com)

To start a new discussion for this project, email Cosmos@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com