Quick Search for:  in language:    
Allows,users,able,send,keystrokes,programs,ru
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Visual Basic Stats

 Code: 3,014,970. lines
 Jobs: 119. postings

 How to support the site

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for Visual Basic
Unroll2 - Update
By Cyber Chris on 7/2


MultilinePWD
By Cyber Chris on 7/2


Click here to see a screenshot of this code!Song/Poem Assistant
By Peter Rowan on 7/2

(Screen Shot)

Click here to see a screenshot of this code!GPA Cal
By KBM-00 on 7/2

(Screen Shot)

Click here to see a screenshot of this code!Connection Via the Telephone line.No internet or cable.Just the telephone line
By Nass ClickMan on 7/2

(Screen Shot)

DBTool
By Make Strömberg on 7/2


Click here to see a screenshot of this code!MSChart Simple Example
By Sebastian Pereira on 7/2

(Screen Shot)

CString v1.5
By Ultimatum on 7/2


Tablature Pro
By Michael McMullen on 7/2


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

clsSendKeys

Print
Email
 

Submitted on: 11/12/1997
By: Steve Register 
Level: Not Given
User Rating: By 104 Users
Compatibility:VB 5.0, VB 6.0

Users have accessed this code 15624 times.
 
 
     Allows users to be able to send keystrokes to dos programs running in a windows95 dos box
 
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, De
'     stination, 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 cl
'     ass module in their code
'
' Side Effects:None that I am aware of
'
'This code is copyrighted and has' limited warranties.Please see http://w
'     ww.Planet-Source-Code.com/vb/scripts/Sho
'     wCode.asp?txtCodeId=749&lngWId;=1'for details.'**************************************

Option Explicit
'local variable(s) to hold property valu
'     e(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 char ' acter 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 t ' ime 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 prop ' erty, 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 propert ' y, on the right side of an assignment. 'Syntax: Debug.Print X.Destination Destination = mvarDestination End Property

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:Bryan 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/21/1999 3:11:00 AM:Marcus
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/23/1999 5:29:00 AM:Alex 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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/4/1999 10:35:00 AM:Andrew 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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/12/1999 1:24:00 AM:Sean 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!!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/31/1999 10:15:00 AM:Job
Can anyone show me how to use this 
routine!
I'd like to sendkeys to DOS 
application on Windows 95 as said.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/31/1999 3:03:00 PM:Job
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/12/1999 4:23:00 PM:Esplin
How did you do the carriage rutrns? 
E-mail me at esplin_9466@yahoo.com thanx
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/6/1999 11:28:00 AM:Jarem
Can someone send me how to use this 
routine please. I'm a newbie to vb.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/13/1999 6:01:00 AM:G.SURIVET
How did you do the F1-F12 keys?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/20/1999 9:59:00 PM:Storm
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 :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/20/1999 10:10:00 PM:Storm
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 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/18/1999 9:12:00 PM:Scriby
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/22/1999 11:23:00 AM:Bill
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/31/2000 4:46:07 PM:AGP
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/6/2001 7:54:42 AM:DECIS
Worked fine, convertd to Java eay peasy 
for a client based app. Thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/20/2001 2:35:43 PM:Spike
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 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/20/2001 2:37:28 PM:Spike
well for some reason it didn't post the 
whole thing, so i'll email it to u AGP.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/25/2002 2:29:38 PM:dintelsis@hotmail.com
Excelent
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/31/2002 10:35:56 PM:Brian 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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/4/2002 3:06:08 PM:David Moberg
Here's the solution to Storm's code: 
Just use
     Sendkeys vbNewLine
to 
send the Enter key.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Visual Basic Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.