Converting KBDLLHOOKSTRUCT(.NET) to KeyEvent/Char(Java), JNA - c#

So, basically what i'm doing is using JNA to set a LowLevelKeyboardProc Keyboard hook, everything works perfectly fine, i can get the values exactly like i want them in java, but the problem i get is when trying to convert to chars, it becomes extremely ennoying handling caps locks, SHIFT keys and tons of other things like everything thats not a-z 0-9 on the keyboard, i was wondering if there is a easier way to do the conversion?
heres the details of what I'm getting from the hook every time a key is pressed
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644967(v=VS.85).aspx
, i Figured it might be best to find a way to manually generate a KeyEvent(Not char, since i need something to handle things like F keys, caps lock button, CTRL button etc etc).
Any help i can get is highly appriciated!.

The Abbot project (http://abbot.sf.net) has a system for mapping keycodes to keychars, using predefined keyboard mappings (it generates a wide variety of keystrokes and records the resulting character output). However, Java does not provide a way for "predicting" the resulting character output given a particular key code.
There may be something within the MS libraries.

Related

What is the most general way to format textbox text value with both static and dynamic elements?

I'm currently working on a form with a bunch of textboxes with quite specific requirements. For example one textbox contain cadastral number and should look like ##:##:#######:~ (last range of digits varies) and also it would be quite nice to see the pattern before you even type anything (if I recall correctly it's called mask). Also giving requirements first two digits always should be 24, so the end result should look shomething like this 24:##:#######:~. Another example is a numeric textbox with units and spaces between big digit numbers (ex 1 000 000 m2). For short this one text box and the others have both static elements (which user should not be able to edit) and dynamic (which makes masked textboxes and similar stuff quite hard to deal with).
So, I've tried different things:
Using maskedTextBox from toolkit package, which turned out bad, because it did poorly handle last part with range of digits, and also when a key was pressed when in the middle of the static mask, it just pushed the carret, but not actually add anything to the text;
Using converters prove quite chalenging at first, but gave remarcable results, it handles variable range of part perfectly, partialy, because of custom converter, but it was difficult to manage things, when user deleted text, because of static parts being integrated in the converter itself;
Using StingFormat for textbox with binding text property was almost useless, although it handle static part quite well, and in the end I couldn't make it work.
Intuition tells me a combination of a custom converter (handling dynamic part) and and StringFormat (handling uneditable static part) should do the job. Maybe the number of requirements is just too much for a simple textbox. Also one thing is bugging me, there could be a sutable existing general solution, that I am not aware of (At first I didn't know Converters was a thing).
Now the question, how would you generally approach this problem? Is there any existing solutions, that with a bit of tweaking would work?

Trouble with sending input via RDP by all known ways

I write a little client using WinForms, C#, AxMsTscNotSafeForScripting tool, and I need to send Win+R on the VM(yes, I know, there's a way to launch app remotely without RunDialog, but I need to).
I quickly found nice lib named InputSimulator, it simulates everything correctly on my main machine, but no effect on VM.
Actually, it can send the whole string into notepad, but when it deals with VirtualKeyCodes, nothing happens.
After drilling Google a bit deeper I found usage of WindowsAPI.SendInput with methods
void PressKey(char ch, bool press)
void KeyDown(ushort scanCode)
void KeyUp(ushort scanCode).
PressKey is working with VM, but KeyDown and KeyUp, which I need, are incorrect. For example, 81 is ScanCode for "Q", but it prints "." in notepad, 82 is for "R", but I get "0" and I get nothing at all with 91, that's for Windows key.
If I press key with a keyboard, everything works, so problem isn't in KeyPreview, EnableWindowsKey, etc settings
Both systems are Win7, Oracle VM VirtualBox.
Even if my way is hopeless, what are another ways to send Win+R programmly to VM? Help will be greatly appreciated!
The reason PressKey is working is that you're supplying an ASCII char, which it expects.
The reason KeyDown and KeyUp are not working is that you're still supplying an ASCII value, but these expect a scan code. Scan codes are not the same as ASCII codes. 82 (0x52) is not "R" in any of the common scancode tables -- in both Table 2 and 3 it is NumPad 0, which is consistent with your observed behavior.
You need to use MapVirtualKey(Ex) to translate your ASCII code or virtual key code into a scan code.
It looks like "R" is 0x13 (in both Table 2 and 3), but the WinKey has a different code in every table, so you should not hard-code the value but get it at runtime using MapVirtualKeyEx.

how to interpret keyeventargs in windows8 consumer preview

There seems to be some changes in the Windows 8 consumer preview with regards to KeyEventArgs. What is the best way to interpret KeyEventArgs and tell what key was pressed or released? I am guessing I need to map the Key (of type VirtualKey) property to the actualy ascii code (I am happy with ascii and not worry about unicode and other keyboard layout). Is there an easier way to manage shift/control and other combined keystrokes?
Does KeyboardEvent work for you?
http://msdn.microsoft.com/en-us/library/windows/apps/hh465793.aspx
UPDATE:
This is only for Javascript.
Leaving this there in case any future ones is looking for this.

System.Windows.Forms.Keys - Lower or Uppercase?

I have been searching around for an answer to this but I can't seem to find anything. Does anyone know if you can determine the letter casing in Keys?
For example:
if (System.Windows.Forms.Keys.A.ToString() == "A")
{
// Upper or Lower?
}
Thanks.
There is no casing, it represents a physical key on your keyboard. Do you see an 'a' and an 'A' on your keyboard?
You can check and see if a Shift key is depressed.
System.Windows.Forms.Keys.A represents the physical key A on your keyboard. It does not have a case. Thus, your question does not make sense.
If you want to check whether the user holds the Shift key on the keybord, there's also System.Windows.Forms.Keys.Shift.
There is no simple mapping between keys and characters. Keyboard layouts can work differently. One example are dead keys. And once you get to IMEs it gets even more complicated. Do not try to duplicate a keyboard layout manually in your application.
If you want to get what character a user entered, handle WM_CHAR, not WM_KEY_DOWN/UP. It's exposed as Control.KeyPress event in winforms.

How Do You Simulate Typing in c#?

I am thinking of making a few video turorials on C#, my problem is that I don't type very fast and I don't want to put the user to sleep as they watch me typing in real-time.
I would like to write a small C# program that will take a line of text and feed it to the keyboard buffer, so that I can simulate keyboard typing.
Does anyone know how to access the keboard buffer to do this?
If this has been done before or if someone knows of an existing program to do this, can you point me in the right direction.
Thanks.
You should use SendKeys, i can show you how to use
here is an example: SendKeys.Send('A');
but u can use it with your own character: SendKeys.Send(CHARACTER HERE);
what happens if we have a string variable, you will get nothing
if it happens use it this way:
string letter = "exampleletter";
foreach (char ch in letter)
SendKeys.Send(ch.ToString());
i hope it works for you
Yogibear
At PDC and other conferences I've been at, they make liberal use of code snippets to quickly drop the new code into place.
I'm not really sure that you can write to keyboardbuffer or something like this
what I know it to send to some windows some keyboard commands
in your case it will be sending keyboard commands to notepad probably
in that case use the function provided above
but I would recommend to cut video parts (typing moments) out of your video
instead of writing code in C#
You could use
SendKeys Class
Provides methods for sending keystrokes to an application.

Categories

Resources