index > Visual C# General > Problem with keybd_event()

Problem with keybd_event()

Hello,

I am using keybd_event to simulate keyboard in applications. Function works perfect in notepad, calc, all windows, etc. but in some directx program it is getting error. On some computers it can type keys but on some others it can not. I have no idea what to do to fixt it or what is missing on those computers.

Anyone has ever met this problem or know how to fix it please ?

Btw that's a part of source

[DllImport("user32.dll")]
private static extern void keybd_event(
byte bVk,
byte bScan,
uint dwFlags,
IntPtr dwExtraInfo
);

private void keybd_press(byte bVk)
{
keybd_event(bVk, 0, 0, new System.IntPtr());
keybd_event(bVk, 0, KEYEVENTF_KEYUP, new System.IntPtr());
}

Quirm

Hi Quirm, in my own experiences in this area I found that just sending the wm_keydown and wm_keyup events was more reliable with DirectX applications. Here is some of the interop code:

public void SendKey(ushort key, IntPtr hWnd)

{

SetActiveWindow(hWnd);

SendMessage(hWnd, WM_KEYDOWN, key, 0);

SendMessage(hWnd, WM_KEYUP, key, 0);

}

[DllImport("user32.dll")] //Set the active window

public static extern IntPtr SetActiveWindow(IntPtr hWnd);

[DllImport("user32.dll")] //sends a windows message to the specified window

public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, int lParam);

//API Constants

public const ushort WM_KEYDOWN = 0x0100;

public const ushort WM_KEYUP = 0x0101;

Hope that helps,

Dennis

Dennis Stone - MSFT
Thanks a lot for your reply. It works great.
Quirm

Dennis - Is there any particular reason you didn't use PostMessage?  I'm looking at the documentation for the two functions and am not seeing why a person would choose one over the other.  Just wondering.

Also, is the key parameter (passed to SendMessage in SendKey()) sill the virtual-key code for a given key?  This was the case for keybd_event.  Again, the documentation for SendMessage isn't really clear on this issue as far as I can tell. 

(Perhaps I should find better documentation!)

Thanks.

josh.butcher
After re-reading the documentation I see that SendMessage waits until the specified window has finished processing the message, whereas PostMessag simply returns without waiting. Is that your reason for using one vs. the other?
josh.butcher
reply 5

You can use google to search for other answers

 

More Articles

• How can I make datagrid display the caption instead of the name o...
• database connection string
• Gif files in database
• Restrict the size of a string
• Custom Marshaling of DateTime
• serialport break state
• Basic dataset question from a VS beginner
• Listview checkboxes misaligned when SmallImageList is set
• HOWTO find a Word document.
• Controls Disappear from WinForms at Design Time
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Whats wrong with MemoryStream???
• The Secret Answer...
• Create a folder with permissions set to
• .Parent
• How i can zip files with SharpZipLib
• C# Hashtable Issue
• datagridview navigation
• Where is best place to declare frequentl
• Finding the computer's country of use
• urlEncode
• embedded .mht
• Form Container
• How to validate correct Enum name?
• GridView
• Key Press Enter - Web Browser

Hot Articles

• Daemonizing a console app
• How to read formatted data from an Excel
• Cannot implicitly convert type 'int' to
• StringDictionary DesignTimeEditor
• app.TaskVisible = false
• movie
• Desktop Sidebar using C#??
• message "Failed to initialize distr
• XML/XSLT -I think I'm going to put a ham
• How to create a guid
• Nested Partial Classes and Designer's
• adding a library in a aspx file
• Retrieve some date from ReportDocument
• insert java script
• binding to Obj.Property.Property

Recommend Articles

• How to post large contents using HttpWeb
• How to get total memory of computer
• Simple 'passing variables between forms'
• create gif
• How do i bind combobox to SortedList
• Project Creation failed error
• How to convert the string to byte in c# ?
• Problem sending e-mail automatically fro
• get the EXE directory from C#
• C# adding a record to an access Database
• Import xls file into dataset
• Are there really no methods that convert
• help needed in creating runtime objects
• Capturing part of a screen
• Datagridviews inC#.net