ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

kbhit.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:   See COPYING in the top level directory
00003  * PROJECT:     ReactOS system libraries
00004  * FILE:        lib/msvcrt/conio/kbhit.c
00005  * PURPOSE:     Checks for keyboard hits
00006  * PROGRAMERS:  Ariadne, Russell
00007  * UPDATE HISTORY:
00008  *              28/12/98: Created
00009  *              27/9/08: An almost 100% working version of _kbhit()
00010  */
00011 
00012 #include <precomp.h>
00013 
00014 static CRITICAL_SECTION CriticalSection;
00015 volatile BOOL CriticalSectionInitialized=FALSE;
00016 
00017 /*
00018  * FIXME Initial keyboard char not detected on first punch
00019  *
00020  * @implemented
00021  */
00022 
00023 int _kbhit(void)
00024 {
00025     PINPUT_RECORD InputRecord = NULL;
00026     DWORD NumberRead = 0;
00027     DWORD EventsRead = 0;
00028     DWORD RecordIndex = 0;
00029     DWORD BufferIndex = 0;
00030     HANDLE StdInputHandle = 0;
00031     DWORD ConsoleInputMode = 0;
00032 
00033     /* Attempt some thread safety */
00034     if (!CriticalSectionInitialized)
00035     {
00036         InitializeCriticalSectionAndSpinCount(&CriticalSection, 0x80000400);
00037         CriticalSectionInitialized = TRUE;
00038     }
00039 
00040     EnterCriticalSection(&CriticalSection);
00041 
00042     if (char_avail)
00043     {
00044         LeaveCriticalSection(&CriticalSection);
00045         return 1;
00046     }
00047 
00048     StdInputHandle = GetStdHandle(STD_INPUT_HANDLE);
00049 
00050     /* Turn off processed input so we get key modifiers as well */
00051     GetConsoleMode(StdInputHandle, &ConsoleInputMode);
00052 
00053     SetConsoleMode(StdInputHandle, ConsoleInputMode & ~ENABLE_PROCESSED_INPUT);
00054 
00055     /* Start the process */
00056     if (!GetNumberOfConsoleInputEvents(StdInputHandle, &EventsRead))
00057     {
00058         LeaveCriticalSection(&CriticalSection);
00059         return 0;
00060     }
00061 
00062     if (!EventsRead)
00063     {
00064         LeaveCriticalSection(&CriticalSection);
00065         return 0;
00066     }
00067 
00068     if (!(InputRecord = (PINPUT_RECORD)malloc(EventsRead * sizeof(INPUT_RECORD))))
00069     {
00070         LeaveCriticalSection(&CriticalSection);
00071         return 0;
00072     }
00073 
00074     if (!ReadConsoleInput(StdInputHandle, InputRecord, EventsRead, &NumberRead))
00075     {
00076         free(InputRecord);
00077         LeaveCriticalSection(&CriticalSection);
00078         return 0;
00079     }
00080 
00081     for (RecordIndex = 0; RecordIndex < NumberRead; RecordIndex++)
00082     {
00083         if (InputRecord[RecordIndex].EventType == KEY_EVENT &&
00084             InputRecord[RecordIndex].Event.KeyEvent.bKeyDown)
00085         {
00086             BufferIndex = 1;
00087             break;
00088         }
00089     }
00090 
00091     free(InputRecord);
00092 
00093     /* Restore console input mode */
00094     SetConsoleMode(StdInputHandle, ConsoleInputMode);
00095 
00096     LeaveCriticalSection(&CriticalSection);
00097 
00098     return BufferIndex;
00099 }
00100 
00101 

Generated on Sun May 27 2012 04:36:27 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.