Can a key- say F3 be bound to a string with C#?

  • Thread starter Thread starter arcticool
  • Start date Start date
A

arcticool

I have a commonly used password that is low security, but part of my
application testing so I want to just bind it somehow to an F-key say F3
and maybe another to F4. So I can click on the field and just hit F3 to
fill in the PW.

If you saw how many times a day I have to do this you would understand
my willingness to code for such a thing!

So can this be done with C#? I do have some basic c# experience and can
put it together with a little guidance.
Thanks,

Jeff
 
So can this be done with C#?

With the greatest of ease.

In the KeyDown or KeyPress event of the TextBox, add the following:

if(e.KeyCode == Keys.F3)

{

textBox1.Text = "Hello"; // or whatever you've called the TextBox

}
 
arcticool said:
I have a commonly used password that is low
security, but part of my application testing so I
want to just bind it somehow to an F-key say F3
[...] So I can click on the field and just hit F3 to
fill in the PW. [...] So can this be done with C#?

Yes, but you'll almost certainly need to use some Window API calls.

Here's how I would do this:

- Use the API function SetWindowsHookEx to create a keyboard hook, so
your application can receive keystrokes even when it is not active.
(This requires quite a lot of code, unfortunately, because the .NET
Framework has no built-in support for hooks. As a cheap alternative,
you could set up a timer that calls GetAsyncKeyState several times per
second to check whether the key you want is being pressed *right
now*.)

- When you get the keystroke you're interested in, use SendKeys (not
an API, but part of the .NET Framework) to send your password
characters to the currently active window.

P.
 
Of course, at that point, you have to ask what the purpose of having a
password is, since the password is now going to be embedded in the assembly
itself, in plain text, no less, for anyone to see with a decompiler (or even
ILDASM).
 
Of course, at that point, you have to ask what the purpose of having a
password is, since the password is now going to be embedded in the
assembly itself, in plain text, no less, for anyone to see with a
decompiler (or even ILDASM).

Of course you're right. I took the OP to be more of a "how do I respond to
the function keys?" question rather than "how can I hard-code my passwords
into my apps?" type of question...

I've done similar things myself, though I tend to wrap such functionality up
in the #if(DEBUG)...#endif syntax to allow me to do stuff in debug mode that
my users can't do in release mode...
 
Yes, but you'll almost certainly need to use some Window API calls.

???

All he wants to do is click on a textbox, hit F3 and have some hard-coded
text appear in that textbox...

In the KeyDown or KeyPress event of the TextBox:

if(e.KeyCode == Keys.F3)
{
textBox1.Text = "Hello"; // or whatever you've called the TextBox
}
 
Mark Rae said:
???

All he wants to do is click on a textbox, hit F3 and have some hard-coded
text appear in that textbox...

In the KeyDown or KeyPress event of the TextBox:

if(e.KeyCode == Keys.F3)
{
textBox1.Text = "Hello"; // or whatever you've called the TextBox
}

It's not clear from the original post whether he wants to do it in
*his* application or in others (eg IE) though. I *suspect* he's talking
about the latter - wanting to write a background program which binds to
F3 whatever app he's using. That's clearly a lot harder - *if* it's
what is meant.
 
It's not clear from the original post whether he wants to do it in
*his* application or in others (eg IE) though. I *suspect* he's talking
about the latter - wanting to write a background program which binds to
F3 whatever app he's using. That's clearly a lot harder - *if* it's
what is meant.

That's not how I read it at all...
I have a commonly used password that is low security, but part of my
application testing so I want to just bind it somehow to an F-key say F3
and maybe another to F4. So I can click on the field and just hit F3 to
fill in the PW.

If you saw how many times a day I have to do this you would understand
my willingness to code for such a thing!

To me, that says he's going through the testing phase and is bored of having
to type in a low-security password many times, and is looking for a way to
click on a text box and have the password filled in by hitting F3 rather
than by constantly typing it out.
 
Mark Rae said:
I have a commonly used password that is low security,
but part of my application testing so I want to just bind
it somehow to an F-key say F3 [...]

To me, that says he's going through the testing phase and
is bored of having to type in a low-security password
many times,

Type it into what? His own application, or SQL Server, or a Web site?

Frankly I felt that making a keypress produce characters in a textbox
is *so* trivial that the poster had to mean doing it in a different
window.

P.
 
Wow, so many replies so soon, I'll have to check in more often :-)

OK, so yes that's all I'm looking for- I'm doing web testing with IE 6,
I don't have access to the source code of the application itself. All I
want to do is to input a password into a text box in IE and I do it so
many times per hour that its terribly redundant to not have it bound
somehow.
Thanks again,

AC
 
... and if it is in fact so trivial, to simply bind a keypress to put
text into an IE6 field (I thought so too!) then perhaps you would be so
kind as to enlighten us as to how it is done. No offense meant, but I
think you will find it to be a less 'trivial' task once you have
acutally tried to find the solution ;-)
Thanks again,

AC
 
arcticool said:
.. and if it is in fact so trivial, to simply bind a keypress to put
text into an IE6 field (I thought so too!) then perhaps you would be so
kind as to enlighten us as to how it is done. No offense meant, but I
think you will find it to be a less 'trivial' task once you have
acutally tried to find the solution ;-)

No-one claimed that that task was trivial - it's just that your
original description wasn't clear enough to distinguish between the
task which *is* trivial (making a text box in your own application do
what you want) and the task which is very hard (making a text box in
another application do what you want).
 
Back
Top