[pascal]
var
g_pDI: IDirectInput8;
g_pKeyboard: IDirectInputDevice8;
begin
if bExclusive then
dwCoopFlags := DISCL_EXCLUSIVE
else
dwCoopFlags := DISCL_NONEXCLUSIVE;

if bForeground then
dwCoopFlags := dwCoopFlags or DISCL_FOREGROUND
else
dwCoopFlags := dwCoopFlags or DISCL_BACKGROUND;

// Disabling the windows key is only allowed only if we are in foreground nonexclusive
if bDisableWindowsKey and not bExclusive and bForeground then
dwCoopFlags := dwCoopFlags or DISCL_NOWINKEY;

// Create a DInput object
hr := DirectInput8Create( HInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, g_pDI, nil );

// Obtain an interface to the system keyboard device.
hr := g_pDI.CreateDevice( GUID_SysKeyboard, g_pKeyboard, nil );

// Set the data format to "keyboard format" - a predefined data format
//
// A data format specifies which controls on a device we
// are interested in, and how they should be reported.
//
// This tells DirectInput that we will be passing an array
// of 256 bytes to IDirectInputDevice::GetDeviceState.
hr := g_pKeyboard.SetDataFormat( c_dfDIKeyboard );

// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr := g_pKeyboard.SetCooperativeLevel( Handle, dwCoopFlags );
[/pascal]