Important alert: (current site time 7/16/2013 4:16:52 AM EDT)
 

VB icon

clsSendKeys

Email
Submitted on: 11/12/1997
By: Steve Register 
Level: Not Given
User Rating: By 6 Users
Compatibility: VB 5.0, VB 6.0
Views: 41137
 
     Allows users to be able to send keystrokes to dos programs running in a windows95 dos box
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :clsSendKeys
'**************************************
none, everything is done in the class module
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: clsSendKeys
' Description:Allows users to be able to send keystrokes to dos programs running in a windows95 dos box
' By: Steve Register
'
' Inputs:This class has one property, Destination, which needs to be the handle returned from the shell function of the dos program or any program 
started with the shell function.
It also has one method called, SendKeys, this is the string to be sent to the destination.
'
' Returns:N/A
'
' Assumes:Nothing except how to use a class module in their code
'
' Side Effects:None that I am aware of
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=749&lngWId=1'for details.'**************************************

Option Explicit
'local variable(s) to hold property value(s)
Private mvarDestination As Long 'local copy
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SHIFT = &H10
Private Declare Function OemKeyScan Lib "user32" (ByVal wOemChar As Integer) As Long
Private Declare Function CharToOem Lib "user32" Alias "CharToOemA" (ByVal lpszSrc As String, ByVal lpszDst As String) As Long
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub SendAKey(ByVal keys As String)
Dim vk%
Dim shiftscan%
Dim scan%
Dim oemchar$
Dim dl&
Dim shiftkey%
' Get the virtual key code for this character
vk% = VkKeyScan(Asc(keys)) And &HFF
' See if shift key needs to be pressed
shiftkey% = VkKeyScan(Asc(keys)) And 256
oemchar$ = " " ' 2 character buffer
' Get the OEM character - preinitialize the buffer
CharToOem Left$(keys, 1), oemchar$
' Get the scan code for this key
scan% = OemKeyScan(Asc(oemchar$)) And &HFF
' Send the key down
If shiftkey% = 256 Then
'if shift key needs to be pressed
shiftscan% = MapVirtualKey(VK_SHIFT, 0)
'press down the shift key
keybd_event VK_SHIFT, shiftscan%, 0, 0
End If
'press key to be sent
keybd_event vk%, scan%, 0, 0
' Send the key up
If shiftkey% = 256 Then
'keyup for shift key
keybd_event VK_SHIFT, shiftscan%, KEYEVENTF_KEYUP, 0
End If
'keyup for key sent
keybd_event vk%, scan%, KEYEVENTF_KEYUP, 0
End Sub
Public Sub SendKeys(ByVal keys As String)
Dim x&, t As Integer
'loop thru string to send one key at a time
For x& = 1 To Len(keys)
'activate target application
AppActivate (mvarDestination)
'send one key to target
SendAKey Mid$(keys, x&, 1)
Next x&
End Sub
Public Property Let Destination(ByVal vData As Long)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Destination = 5
mvarDestination = vData
End Property
Public Property Get Destination() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Destination
Destination = mvarDestination
End Property


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/19/1999 10:54:00 AMBryan Kraus

The code worked great. However, I need
to use it on an NT box and I cannot send
a carriage return (ideas?) I had no problems
getting it to work with 95.


(If this comment was disrespectful, please report it.)

 
4/21/1999 3:11:00 AMMarcus

Your code works well and i can't see
much wrong with it. The problem is I
can't seem to send carriage returns,
like the person before.

Are there any fixes to this?

(If this comment was disrespectful, please report it.)

 
4/23/1999 5:29:00 AMAlex Ruimy

Hi! I can't get this code working for my VB Class in school. Can you send me a project already compiled with the code, so i can look at it and see what u did that I didn't do. Thanx.

Alex
(If this comment was disrespectful, please report it.)

 
5/4/1999 10:35:00 AMAndrew Clarke

I was able to get the code to send characters, but not carriage returns or alt keys, etc.
How can I do this?

Thanks,
Andrew
(If this comment was disrespectful, please report it.)

 
5/12/1999 1:24:00 AMSean Burns

Why on earth has somebody made this task seem hard??? All you need to do is activate the app in question and then use the sendkeys command built in VB!!!
(If this comment was disrespectful, please report it.)

 
5/31/1999 10:15:00 AMJob

Can anyone show me how to use this routine!
I'd like to sendkeys to DOS application on Windows 95 as said.
(If this comment was disrespectful, please report it.)

 
5/31/1999 3:03:00 PMJob

Finally I can only make the Carriage Returns to go with StringTosend & chr$(13)

But ONLY WORKS ON NORMAL MS-DOS Prompt screen NOT full maximized screen, when run in exclusive full screen dos mode only text can be sent CR is dropped again. Why????

Anybody please fix this..!!
Job.
(If this comment was disrespectful, please report it.)

 
7/12/1999 4:23:00 PMEsplin

How did you do the carriage rutrns? E-mail me at esplin_9466@yahoo.com thanx
(If this comment was disrespectful, please report it.)

 
9/6/1999 11:28:00 AMJarem

Can someone send me how to use this routine please. I'm a newbie to vb.
(If this comment was disrespectful, please report it.)

 
9/13/1999 6:01:00 AMG.SURIVET

How did you do the F1-F12 keys?
(If this comment was disrespectful, please report it.)

 
9/20/1999 9:59:00 PMStorm

The code works great though i modified
it a bit :) PS the problem is with
sending the text to the next line is
easier then you make it out to be :)

And not to mention most of you witch
are newbies cant figure it out so im
going out of my way to do this, one time

Use his code and and your own function

example

Public function SendNow
Dim enter
enter = Chr(13) & Chr(10)
SendKeys txtSend.Text & enter
end function

Then call it using a command button

SendNow

Thats it, easy as cake have fun with
your learning hahaaha

Greets to all you lamerz :)


(If this comment was disrespectful, please report it.)

 
9/20/1999 10:10:00 PMStorm

As for the F1 - F12 Keys

Use this method

SendKeys {F1}
SendKeys {F2}
SendKeys {F3}

Im sure you get the point eh :)

Lamerz word of advice do some reading
it will be to your advantage :)

or to use the alt function
SendKeys "%{F1}"

So on

(If this comment was disrespectful, please report it.)

 
9/26/1999 7:54:00 AM.tOm

how can i send cursorkeys to the class?
{up} or {down} doesnt work, just gives me beeps! :(

.tOm
(If this comment was disrespectful, please report it.)

 
10/18/1999 9:12:00 PMScriby

For all of you who have been trying, Storm needs to do a little research. The method he used will only send the "/" character to the MS-DOS window, would of helped a little if he'd of tested it out instead of just riding his ego trip claiming all others to be lamers . . .

All that run-around on the code was needed! And, if you can just write a .bat file with VB and execute it, you don't have to worry about sending strokes to the DOS window at all (unless of course you need to enter non-command line text).

If you have any questions feel free to e-mail me,

Scriby
(If this comment was disrespectful, please report it.)

 
10/22/1999 11:23:00 AMBill

Pretty cool program. I'm taking a visual basic class at college. How would you go about writing all keys typed and then log them into a file? Is it possible to log all keys type in any application under windows?
Bill


(If this comment was disrespectful, please report it.)

 
5/31/2000 4:46:07 PMAGP

about sending keystrokes to an MSDOS window shelled out by VB. My problem is this, my program has several steps (say step 1 thru 6). On the 3rd step, i shell out to an MSDOS box and run a batch file on my network. However, steps 4 thru 6 are dependent on step 3 finishing, so ive used a WaitForShelled code that basically waits for the shelled program to be closed, and then the consecutive VB code is executed.
well, while in the MSDOS batch routine, i get two message prompts saying
"MyProgram.bat may not run well unless it is run in MS-DOS mode. Would
you like to create a shortcut to this program that will run in MS-DOS mode?
and then i have a choice of Yes (Alt-Y) or No(Alt-N). At this point i want to
choose No and then proceed with the rest of the batch file. so how do i detect the message box and how do i send it the "No" command for the two message boxes, all while halting any further VB code until the MSDOS routine is done.
Thanx
AGP
(If this comment was disrespectful, please report it.)

 
2/6/2001 7:54:42 AMDECIS

Worked fine, convertd to Java eay peasy for a client based app. Thanks
(If this comment was disrespectful, please report it.)

 
10/20/2001 2:35:43 PMSpike

AGP, to detect the message box, get a windows spy, u can find them at download.com, but one should have come in the tools with vb. get one of those, have the message box window open, then u have to find out the windows class with the spy, so do that (it is different for each spy). then in a module put declare the code: Public Declare Function FindWindow Lib
(If this comment was disrespectful, please report it.)

 
10/20/2001 2:37:28 PMSpike

well for some reason it didn't post the whole thing, so i'll email it to u AGP.
(If this comment was disrespectful, please report it.)

 
8/25/2002 2:29:38 PMdintelsis@hotmail.com

Excelent
(If this comment was disrespectful, please report it.)

 
8/31/2002 10:35:56 PMBrian Payne

Hey man this is EXACTLY what i was looking for, because it actually TELLS WINDOWS that the key is PRESSED DOWN... Now if all these wannabe programmers who wrote in your posts above would understand your API calls like I do, then they would realize the myriad uses this can be applied to.

FOR EXAMPLE : ********* I was able to create a SPY PROGRAM that steals Outlook Express emails with yoru code. THANKS!! 6 from me!
(If this comment was disrespectful, please report it.)

 
10/4/2002 3:06:08 PMDavid Moberg

Here's the solution to Storm's code: Just use
Sendkeys vbNewLine
to send the Enter key.
(If this comment was disrespectful, please report it.)

 
8/26/2004 1:55:30 AM

the code is cool.

if i want to send the keystrokes to DOS program, then i need to use this class, cause the vb built-in sendkeys unable to send character to DOS program.

Anyway, how to send Tab Key using this class?

plz reply me soon, thanks.
(If this comment was disrespectful, please report it.)

 
5/30/2005 4:29:01 AMMilind Patil

Code works file with the DOS window where system is taking input from user (where Cursor is blining). But
The problem with code is , when i fire DIR/s Command on Command prompt at this point there is no blinking cursor which accepts our input now i want to fire CONTROL+X . In your code , you place characters in the string one by one but what to do in the situation like DIR/S where some processing is going on already ?
(If this comment was disrespectful, please report it.)

 
7/7/2005 1:58:32 PM

Steve,
Im new to Class Modules. Can you send some sample code? I can't seem to put characters greater than 255 in the string.
Thanks!
(If this comment was disrespectful, please report it.)

 
7/7/2005 2:01:29 PM

Steve,
I'm new to Class Modules. Can you send some sample code? I'm having trouble getting a value greater than 255 in the string that is passed to SendAKey.
Thanks!
Rob
(If this comment was disrespectful, please report it.)

 
12/18/2005 1:16:28 PMAndrew

Hi
I would like to have a simple code in VBscript, which detects when either Shift key is held down, while I click on a button object. The leftButtonUp code then would make a decision based on whether the shift was pressed during button pressure or not.

I do not wish to have any other key pressed together with shift.

I can't seem to find anything on the Internet which does exactly this in the VBscript environment. (the container, where VBscript is used is a proprietary graphics package.)

Any ideas on this would be more than welcome!
Andrew
(If this comment was disrespectful, please report it.)

 
4/17/2006 11:32:05 PMkarthikeyan

really a good useful code
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.