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.
Related
I need to automate some repetive tasks performed in an application installed in my machine. I'm coding in C# and using the library TestStack.White I can key in keyboard inputs like SHIFT or RETURN but I can´t figure out how to send a key combo. I must send SHIFT+RETURN but this keystroke is not available in TestStack.White as far as I know. How to do it? Maybe it is easier using Windows.Forms.SendKeys... Thank you in advance.
ts_ui_items.TextBox textBox = characteristics_window.Get(ts_ui_items.Finders.SearchCriteria.ByClassName("Edit"));
textBox.Text = "something";
ts.InputDevices.Keyboard.Instance.HoldKey(ts.WindowsAPI.KeyboardInput.SpecialKeys.SHIFT);
textBox.KeyIn(ts.WindowsAPI.KeyboardInput.SpecialKeys.RETURN);
ts.InputDevices.Keyboard.Instance.LeaveKey(ts.WindowsAPI.KeyboardInput.SpecialKeys.SHIFT);
textBox.Text = "nice";
This piece of code outputs a text in the application's window in its text box like:
something
nice
This is what I tried and it kind of worked but I think this is not the optimal solution....
is it possible to slow the Sendkeys feature down, when I've set that it sends the keys out of a textbox?
So I mean that it takes longer / that there is a delay before sending each letter?
Ive also thought about if it is possible to have to press a key for sending each line.
I prefer the first option. Hope that is even technically possible.
Thank you for reading this
I've got the answer!
I bet the most people don't need help with this, but why shouldn't I publish the answer I found?
Code:
foreach(char uga in rbox.Text)
{
System.Threading.Thread.Sleep(50);
SendKeys.SendWait(uga.ToString());
}
If you got {ENTER} in the Textbox/String, then do this:
{ENTER} has an alternative keyword.
{ENTER} has "~". Writing the {example} keywords in the string will not work!
Just ENTER has this :( And some other keys, but they dont have keywords in these things: {example}
This site did help me!
This question is related to the codebreaker example found in the RSpec book.
See code here: https://github.com/kristianmandrup/rspec-book-codebreaker/tree/master/features
I was wondering if anyone has made a full solution for the codebreaker game.
I am keen to see how to test the outer loop, which decides if the game should continue or not.
The functionality may look like this
You found the secret code!
Would you like to try again? : n
Good-bye
If the user selects ‘y’ then the game starts again.
I am interested to see the scenarios and step definitions.
Would it be something like:
Scenario: user finds secret code
Given the secret code is "1234"
When I guess "1234"
Then I should see "You found the secret code!"
And I should see "Would you like to try again?"
Another spec file:
Background: Found secret code
Given the secret code is "1234"
And I guess "1234"
Scenario: user chooses to quit the game
Given I see "Would you like to try again?"
When I enter "n"
Then I should see "Good-bye"
Scenario: user chooses to continue with the game
Given I see "Would you like to try again?"
When I enter "y"
Then I should see "Enter guess:"
As seen in the link above, currently a double (mock) output object is passed in. All output is captured by this double and then is used in assertions. Input is a bit different. When the application displays "Would you like to try again?", then the input mock needs reply with 'y' or 'n', depending on the test. Perhaps this is not the way to go about it?
There is something similar to mimic a user guessing, that step definition looks like this:
When /^I guess "([^\"]*)"$/ do |guess|
#game.guess(guess)
end
This is the current main method:
def generate_secret_code
options = %w[1 2 3 4 5 6]
(1..4).map { options.delete_at(rand(options.length))}.join
end
game = Codebreaker::Game.new(STDOUT)
secret_code = generate_secret_code
at_exit { puts "\n***\nThe secret code was: #{secret_code}\n***" }
game.start(secret_code)
while guess = gets.chomp
game.guess(guess)
end
The outer loop was added without any tests. I would like to know how to deal with the gets.chomp - how can I 'mock' that behaviour? Is this how to handle interactive input form the console?
I am actually using SpecFlow and C#, if possible provide a solution for C#.
A Ruby solution may also help...
Thanks
It is possible to launch a console application and attach to its standard input and output streams. See the example in the documentation of ProcessStartInfo.RedirectStandardInput.
A cleaner way (which would also make the tests faster, but wouldn't qualify as full end-to-end system testing) would be to restructure the application somewhat, so that it uses regular Stream objects instead of Console.In and Console.Out; the main() method can pass these classes to the rest of the application. Then, your tests can invoke the other methods directly and pass MemoryStream objects as the input/output streams.
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.
I have a form which I read data to textbox from a barcode reader.
and there are some codded barcodes like this
W12346S1 is first step of a work
W12346S2 is second step of a work
W12346S3 is third step of a work
...
U123 is a user he read his code to make process
M456 is a machine user do the work on this machine.
so I want to write data to true textboxes from firs char (W, U, M) in form_KeyDown() event or one different.
(
true textboxes mean if user read a barcode which start with W key let the program write the barcode data to "work tekxtbox" or if he read abarcode which start with U program will write the barcode data to user textbox etc...
)
I wanna make this let the codes select its own textboxes. what is the way?
note: if I use textbox1.Text += e.KeyData.ToString();
the output is : ShiftKey, ShiftW, ShiftD1D2D3D4D6ShiftKey, ShiftS, ShiftD2 W12346S2
for W12346S2
Can't you just read in the text and have something like this:
string FirstChar = BarcodeString.Substring(0,1);
if (FirstChar.Equals("W"))
WorkTextBox.Text = BarcodeString;
if (FirstChar.Equals("U"))
UserTextBox.Text = BarcodeString;
Can input from your barcode reader be distinguished from typed keystrokes? If so, I would recommend that incoming barcodes not be handled by the keystroke handler, but instead use their own special handler which will wait until it has scanned an entire barcodes and then stick it in an appropriate box.
If the input from your reader looks like keystrokes, things are apt to be a little more tricky. You may want to intercept all keystrokes going to your form, look at each keystroke, determine whether it looks like it might be part of a barcode, and buffer it if so. Any time you determine that the buffered data isn't part of a barcode, either because of following characters or because a timer expires, fire your own keystroke events to re-issue the keystrokes. Ensuring that all keystrokes are handled in order may be a little tricky, but hopefully not too bad. It will probably be easier to prevent keystrokes from the barcode reader from going into an inappropriate field, than to provide a good user experience after they do.