How to read from Keyboard in Native Mode

Hi,

I am writing a Native Application. I want to read from Keyboard. I have written following code. but NtReadFile fails. can anybody tell me what’s wrong with it.

NTSTATUS Status;
UNICODE_STRING UnicodeFilespec;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE FileHandle;
IO_STATUS_BLOCK Iosb;
ULONG MessageLength = strlen(Message);
KEYBOARD_INPUT_DATA kbInputData;
ULONG uiSize = 0, uiByteOffset = 0;
RtlInitUnicodeString(&UnicodeFilespec, L"\Device\KeyboardClass0");
InitializeObjectAttributes(&ObjectAttributes, // ptr to structure
&UnicodeFilespec, // ptr to file spec
OBJ_CASE_INSENSITIVE, // attributes
NULL, // root directory handle
NULL ); // ptr to security descriptor
Status = NtCreateFile(&FileHandle, // returned file handle
( GENERIC_READ | SYNCHRONIZE | GENERIC_WRITE ), // desired access
&ObjectAttributes, // ptr to object attributes
&Iosb, // ptr to I/O status block
0, // allocation size
FILE_ATTRIBUTE_NORMAL, // file attributes
FILE_SHARE_WRITE /*| FILE_SHARE_READ*/, // share access
FILE_OPEN, // create disposition
FILE_DIRECTORY_FILE, // create options
NULL, // ptr to extended attributes
0); // length of ea buffer
if( !NT_SUCCESS(Status) )
{
DisplayString ( L"\r\n ***NtCreateFile: Unable to create keyboard device *" );
swprintf ( szwError, L" Status = %0x “, Status );
DisplayString ( szwError );
}
DisplayString ( L”\r\n ***Press a Key" );

uiSize = sizeof ( kbInputData );
memset( &kbInputData, 0, sizeof( kbInputData ) );
Status = NtReadFile ( FileHandle, // file Handle
0, // event Handle
NULL, // APC entry point
NULL, // APC context
&Iosb, // IOSB address
&kbInputData, // ptr to data buffer
uiSize, // length
&uiByteOffset, // byte offset
NULL ); // key
if ( ! NT_SUCCESS ( Status ) )
{
DisplayString ( L"\r\nNtReadFile to Read From keyboard is FAILED" );
swprintf ( szwError, L" Status = %0x ", Status );
DisplayString ( szwError );
//printf(“NtWriteFile request failed 0x%0x\n”, Status);

}
swprintf ( szwError, L" kbInputData.MakeCode = %0x “, kbInputData.MakeCode );
DisplayString ( szwError );
DisplayString ( L”\r\n ***NtReadFile FINISHED" );
NtClose ( FileHandle );

Prithvi


Yahoo! Photos
Ring in the New Year with Photo Calendars. Add photos, events, holidays, whatever.

Well how do you know the device is KeyboardClass0? Systems can have
multiple keyboards, or zero keyboards in the case of headless. Even if you
get this working for some class of systems, what are you going to do about
“lights out operations” where the computer is in a closet and the monitor is
turned off. I know of a small software firm that did this, and was sued
after they sold software licenses to a large company. It turns out that a
power glitch caused a system in the midwest to reboot, unfortunately if hung
by this stuff, and the large company sent someone from New York to see what
was wrong. In the end the software company refunded all the sales (a huge
loss) and paid for the cost of the trip to the midwest.

Why do you think you need this, there are other approaches to gettiug
information at boot time, explain your needs to the group, there may be an
answer that works.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Pruthviraj Kajale” wrote in message
news:xxxxx@ntdev…
> Hi,
>
> I am writing a Native Application. I want to read from Keyboard. I have
> written following code. but NtReadFile fails. can anybody tell me what’s
> wrong with it.
>
> NTSTATUS Status;
> UNICODE_STRING UnicodeFilespec;
> OBJECT_ATTRIBUTES ObjectAttributes;
> HANDLE FileHandle;
> IO_STATUS_BLOCK Iosb;
> ULONG MessageLength = strlen(Message);
> KEYBOARD_INPUT_DATA kbInputData;
> ULONG uiSize = 0, uiByteOffset = 0;
> RtlInitUnicodeString(&UnicodeFilespec, L"\Device\KeyboardClass0");
> InitializeObjectAttributes(&ObjectAttributes, // ptr to structure
> &UnicodeFilespec, // ptr to file spec
> OBJ_CASE_INSENSITIVE, // attributes
> NULL, // root directory handle
> NULL ); // ptr to security descriptor
> Status = NtCreateFile(&FileHandle, // returned file handle
> ( GENERIC_READ | SYNCHRONIZE | GENERIC_WRITE ), // desired access
> &ObjectAttributes, // ptr to object attributes
> &Iosb, // ptr to I/O status block
> 0, // allocation size
> FILE_ATTRIBUTE_NORMAL, // file attributes
> FILE_SHARE_WRITE /| FILE_SHARE_READ/, // share access
> FILE_OPEN, // create disposition
> FILE_DIRECTORY_FILE, // create options
> NULL, // ptr to extended attributes
> 0); // length of ea buffer
> if( !NT_SUCCESS(Status) )
> {
> DisplayString ( L"\r\n *NtCreateFile: Unable to create keyboard device
> " );
> swprintf ( szwError, L" Status = %0x “, Status );
> DisplayString ( szwError );
> }
> DisplayString ( L”\r\n
Press a Key" );
>
> uiSize = sizeof ( kbInputData );
> memset( &kbInputData, 0, sizeof( kbInputData ) );
> Status = NtReadFile ( FileHandle, // file Handle
> 0, // event Handle
> NULL, // APC entry point
> NULL, // APC context
> &Iosb, // IOSB address
> &kbInputData, // ptr to data buffer
> uiSize, // length
> &uiByteOffset, // byte offset
> NULL ); // key
> if ( ! NT_SUCCESS ( Status ) )
> {
> DisplayString ( L"\r\nNtReadFile to Read From keyboard is FAILED" );
> swprintf ( szwError, L" Status = %0x “, Status );
> DisplayString ( szwError );
> //printf(“NtWriteFile request failed 0x%0x\n”, Status);
>
> }
> swprintf ( szwError, L” kbInputData.MakeCode = %0x “,
> kbInputData.MakeCode );
> DisplayString ( szwError );
> DisplayString ( L”\r\n ***NtReadFile FINISHED" );
> NtClose ( FileHandle );
>
>
> Prithvi
>
>
>
> ---------------------------------
> Yahoo! Photos
> Ring in the New Year with Photo Calendars. Add photos, events, holidays,
> whatever.

Hi,

I am assuming that there is only one keyboard. In that case Keyboardclass0 will work. So I need the method to read the key. Please help

Prithvi
Don Burn wrote:
Well how do you know the device is KeyboardClass0? Systems can have
multiple keyboards, or zero keyboards in the case of headless. Even if you
get this working for some class of systems, what are you going to do about
“lights out operations” where the computer is in a closet and the monitor is
turned off. I know of a small software firm that did this, and was sued
after they sold software licenses to a large company. It turns out that a
power glitch caused a system in the midwest to reboot, unfortunately if hung
by this stuff, and the large company sent someone from New York to see what
was wrong. In the end the software company refunded all the sales (a huge
loss) and paid for the cost of the trip to the midwest.

Why do you think you need this, there are other approaches to gettiug
information at boot time, explain your needs to the group, there may be an
answer that works.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Pruthviraj Kajale”
wrote in message
news:xxxxx@ntdev…
> Hi,
>
> I am writing a Native Application. I want to read from Keyboard. I have
> written following code. but NtReadFile fails. can anybody tell me what’s
> wrong with it.
>
> NTSTATUS Status;
> UNICODE_STRING UnicodeFilespec;
> OBJECT_ATTRIBUTES ObjectAttributes;
> HANDLE FileHandle;
> IO_STATUS_BLOCK Iosb;
> ULONG MessageLength = strlen(Message);
> KEYBOARD_INPUT_DATA kbInputData;
> ULONG uiSize = 0, uiByteOffset = 0;
> RtlInitUnicodeString(&UnicodeFilespec, L"\Device\KeyboardClass0");
> InitializeObjectAttributes(&ObjectAttributes, // ptr to structure
> &UnicodeFilespec, // ptr to file spec
> OBJ_CASE_INSENSITIVE, // attributes
> NULL, // root directory handle
> NULL ); // ptr to security descriptor
> Status = NtCreateFile(&FileHandle, // returned file handle
> ( GENERIC_READ | SYNCHRONIZE | GENERIC_WRITE ), // desired access
> &ObjectAttributes, // ptr to object attributes
> &Iosb, // ptr to I/O status block
> 0, // allocation size
> FILE_ATTRIBUTE_NORMAL, // file attributes
> FILE_SHARE_WRITE /| FILE_SHARE_READ/, // share access
> FILE_OPEN, // create disposition
> FILE_DIRECTORY_FILE, // create options
> NULL, // ptr to extended attributes
> 0); // length of ea buffer
> if( !NT_SUCCESS(Status) )
> {
> DisplayString ( L"\r\n *NtCreateFile: Unable to create keyboard device
> " );
> swprintf ( szwError, L" Status = %0x “, Status );
> DisplayString ( szwError );
> }
> DisplayString ( L”\r\n
Press a Key" );
>
> uiSize = sizeof ( kbInputData );
> memset( &kbInputData, 0, sizeof( kbInputData ) );
> Status = NtReadFile ( FileHandle, // file Handle
> 0, // event Handle
> NULL, // APC entry point
> NULL, // APC context
> &Iosb, // IOSB address
> &kbInputData, // ptr to data buffer
> uiSize, // length
> &uiByteOffset, // byte offset
> NULL ); // key
> if ( ! NT_SUCCESS ( Status ) )
> {
> DisplayString ( L"\r\nNtReadFile to Read From keyboard is FAILED" );
> swprintf ( szwError, L" Status = %0x “, Status );
> DisplayString ( szwError );
> //printf(“NtWriteFile request failed 0x%0x\n”, Status);
>
> }
> swprintf ( szwError, L” kbInputData.MakeCode = %0x “,
> kbInputData.MakeCode );
> DisplayString ( szwError );
> DisplayString ( L”\r\n ***NtReadFile FINISHED" );
> NtClose ( FileHandle );
>
>
> Prithvi
>
>
>
> ---------------------------------
> Yahoo! Photos
> Ring in the New Year with Photo Calendars. Add photos, events, holidays,
> whatever.


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping

Even on a machine with only one *physical* keyboard attached, there can be multiple /virtual/ keyboards which are also enumerated as keyboards on the machine. For instance on XP, it is more then likely that KeyboardClass0 is the terminal services redirected virtual keyboard, not a physical keyboard. You cannot make this assumption that there is a mapping to hard coded name. You need to use the device interface and you *need* to read from all the keyboards.

I used to own all of the input stacks in Windows. I have debugged these types of issues and assumptions for years, I am not just some chicken little screaming at the sky ;).

d


From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Pruthviraj Kajale
Sent: Saturday, December 24, 2005 5:08 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] How to read from Keyboard in Native Mode

Hi,
?
I am assuming that there is only one keyboard. In that case Keyboardclass0 will work. So I need the method to read the key. Please help
?
Prithvi
Don Burn wrote:
Well how do you know the device is KeyboardClass0? Systems can have
multiple keyboards, or zero keyboards in the case of headless. Even if you
get this working for some class of systems, what are you going to do about
“lights out operations” where the computer is in a closet and the monitor is
turned off. I know of a small software firm that did this, and was sued
after they sold software licenses to a large company. It turns out that a
power glitch caused a system in the midwest to reboot, unfortunately if hung
by this stuff, and the large company sent someone from New York to see what
was wrong. In the end the software company refunded all the sales (a huge
loss) and paid for the cost of the trip to the midwest.

Why do you think you need this, there are other approaches to gettiug
information at boot time, explain your needs to the group, there may be an
answer that works.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Pruthviraj Kajale” wrote in message
news:xxxxx@ntdev…
> Hi,
>
> I am writing a Native Application. I want to read from Keyboard. I have
> written following code. but NtReadFile fails. can anybody tell me what’s
> wrong with it.
>
> NTSTATUS Status;
> UNICODE_STRING UnicodeFilespec;
> OBJECT_ATTRIBUTES ObjectAttributes;
> HANDLE FileHandle;
> IO_STATUS_BLOCK Iosb;
> ULONG MessageLength = strlen(Message);
> KEYBOARD_INPUT_DATA kbInputData;
> ULONG uiSize = 0, uiByteOffset = 0;
> RtlInitUnicodeString(&UnicodeFilespec, L"\Device\KeyboardClass0");
> InitializeObjectAttributes(&ObjectAttributes, // ptr to structure
> &UnicodeFilespec, // ptr to file spec
> OBJ_CASE_INSENSITIVE, // attributes
> NULL, // root directory handle
> NULL ); // ptr to security descriptor
> Status = NtCreateFile(&FileHandle, // returned file handle
> ( GENERIC_READ | SYNCHRONIZE | GENERIC_WRITE ), // desired access
> &ObjectAttributes, // ptr to object attributes
> &Iosb, // ptr to I/O status block
> 0, // allocation size
> FILE_ATTRIBUTE_NORMAL, // file attributes
> FILE_SHARE_WRITE /| FILE_SHARE_READ/, // share access
> FILE_OPEN, // create disposition
> FILE_DIRECTORY_FILE, // create options
> NULL, // ptr to extended attributes
> 0); // length of ea buffer
> if( !NT_SUCCESS(Status) )
> {
> DisplayString ( L"\r\n *NtCreateFile: Unable to create keyboard device
> " );
> swprintf ( szwError, L" Status = %0x “, Status );
> DisplayString ( szwError );
> }
> DisplayString ( L”\r\n
Press a Key" );
>
> uiSize = sizeof ( kbInputData );
> memset( &kbInputData, 0, sizeof( kbInputData ) );
> Status = NtReadFile ( FileHandle, // file Handle
> 0, // event Handle
> NULL, // APC entry point
> NULL, // APC context
> &Iosb, // IOSB address
> &kbInputData, // ptr to data buffer
> uiSize, // length
> &uiByteOffset, // byte offset
> NULL ); // key
> if ( ! NT_SUCCESS ( Status ) )
> {
> DisplayString ( L"\r\nNtReadFile to Read From keyboard is FAILED" );
> swprintf ( szwError, L" Status = %0x “, Status );
> DisplayString ( szwError );
> //printf(“NtWriteFile request failed 0x%0x\n”, Status);
>
> }
> swprintf ( szwError, L” kbInputData.MakeCode = %0x “,
> kbInputData.MakeCode );
> DisplayString ( szwError );
> DisplayString ( L”\r\n ***NtReadFile FINISHED" );
> NtClose ( FileHandle );
>
>
> Prithvi
>
>
>
> ---------------------------------
> Yahoo! Photos
> Ring in the New Year with Photo Calendars. Add photos, events, holidays,
> whatever.


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

________________________________________
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping — Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are currently subscribed to ntdev as: xxxxx@microsoft.com To unsubscribe send a blank email to xxxxx@lists.osr.com

One way to handle this is to insist that the user’s machine has
an USB keyboard and mouse - in which case the keyboard i/o ports
are up for grabs. By the way SoftICE handles both keyboards
relatively well from very early at boot time, so, it can be
done; but it takes a bit of delicate surgery.

Alberto.

----- Original Message -----
From: “Doron Holan”
To: “Windows System Software Devs Interest List”

Sent: Saturday, December 24, 2005 12:52 PM
Subject: RE: [ntdev] How to read from Keyboard in Native Mode

Even on a machine with only one physical keyboard attached,
there can be multiple /virtual/ keyboards which are also
enumerated as keyboards on the machine. For instance on XP, it
is more then likely that KeyboardClass0 is the terminal services
redirected virtual keyboard, not a physical keyboard. You
cannot make this assumption that there is a mapping to hard
coded name. You need to use the device interface and you need
to read from all the keyboards.

I used to own all of the input stacks in Windows. I have
debugged these types of issues and assumptions for years, I am
not just some chicken little screaming at the sky ;).

d


From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
Pruthviraj Kajale
Sent: Saturday, December 24, 2005 5:08 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] How to read from Keyboard in Native Mode

Hi,

I am assuming that there is only one keyboard. In that case
Keyboardclass0 will work. So I need the method to read the key.
Please help

Prithvi
Don Burn wrote:
Well how do you know the device is KeyboardClass0? Systems can
have
multiple keyboards, or zero keyboards in the case of headless.
Even if you
get this working for some class of systems, what are you going
to do about
“lights out operations” where the computer is in a closet and
the monitor is
turned off. I know of a small software firm that did this, and
was sued
after they sold software licenses to a large company. It turns
out that a
power glitch caused a system in the midwest to reboot,
unfortunately if hung
by this stuff, and the large company sent someone from New York
to see what
was wrong. In the end the software company refunded all the
sales (a huge
loss) and paid for the cost of the trip to the midwest.

Why do you think you need this, there are other approaches to
gettiug
information at boot time, explain your needs to the group, there
may be an
answer that works.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Pruthviraj Kajale” wrote in message
news:xxxxx@ntdev…
> Hi,
>
> I am writing a Native Application. I want to read from
> Keyboard. I have
> written following code. but NtReadFile fails. can anybody tell
> me what’s
> wrong with it.
>
> NTSTATUS Status;
> UNICODE_STRING UnicodeFilespec;
> OBJECT_ATTRIBUTES ObjectAttributes;
> HANDLE FileHandle;
> IO_STATUS_BLOCK Iosb;
> ULONG MessageLength = strlen(Message);
> KEYBOARD_INPUT_DATA kbInputData;
> ULONG uiSize = 0, uiByteOffset = 0;
> RtlInitUnicodeString(&UnicodeFilespec,
> L"\Device\KeyboardClass0");
> InitializeObjectAttributes(&ObjectAttributes, // ptr to
> structure
> &UnicodeFilespec, // ptr to file spec
> OBJ_CASE_INSENSITIVE, // attributes
> NULL, // root directory handle
> NULL ); // ptr to security descriptor
> Status = NtCreateFile(&FileHandle, // returned file handle
> ( GENERIC_READ | SYNCHRONIZE | GENERIC_WRITE ), // desired
> access
> &ObjectAttributes, // ptr to object attributes
> &Iosb, // ptr to I/O status block
> 0, // allocation size
> FILE_ATTRIBUTE_NORMAL, // file attributes
> FILE_SHARE_WRITE /| FILE_SHARE_READ/, // share access
> FILE_OPEN, // create disposition
> FILE_DIRECTORY_FILE, // create options
> NULL, // ptr to extended attributes
> 0); // length of ea buffer
> if( !NT_SUCCESS(Status) )
> {
> DisplayString ( L"\r\n *NtCreateFile: Unable to create
> keyboard device
> " );
> swprintf ( szwError, L" Status = %0x “, Status );
> DisplayString ( szwError );
> }
> DisplayString ( L”\r\n
Press a Key" );
>
> uiSize = sizeof ( kbInputData );
> memset( &kbInputData, 0, sizeof( kbInputData ) );
> Status = NtReadFile ( FileHandle, // file Handle
> 0, // event Handle
> NULL, // APC entry point
> NULL, // APC context
> &Iosb, // IOSB address
> &kbInputData, // ptr to data buffer
> uiSize, // length
> &uiByteOffset, // byte offset
> NULL ); // key
> if ( ! NT_SUCCESS ( Status ) )
> {
> DisplayString ( L"\r\nNtReadFile to Read From keyboard is
> FAILED" );
> swprintf ( szwError, L" Status = %0x “, Status );
> DisplayString ( szwError );
> //printf(“NtWriteFile request failed 0x%0x\n”, Status);
>
> }
> swprintf ( szwError, L” kbInputData.MakeCode = %0x “,
> kbInputData.MakeCode );
> DisplayString ( szwError );
> DisplayString ( L”\r\n ***NtReadFile FINISHED" );
> NtClose ( FileHandle );
>
>
> Prithvi
>
>
>
> ---------------------------------
> Yahoo! Photos
> Ring in the New Year with Photo Calendars. Add photos, events,
> holidays,
> whatever.


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to
xxxxx@lists.osr.com


Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping —
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 You are currently
subscribed to ntdev as: xxxxx@microsoft.com To unsubscribe
send a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: unknown lmsubst tag
argument: ‘’
To unsubscribe send a blank email to
xxxxx@lists.osr.com