For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > March 2004 > How to remotely activate Ctrl-C ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to remotely activate Ctrl-C ?
Jack

2004-03-28, 9:58 pm

Hello,
Here is the scenario:
1. My app is able to accept hotkey: let say Alt + D and is sitting in the
background.
2. User opens some text document, highlight the text and hits Alt+D
3. My app receives Alt+D and now it wants to put selected text to Clipboard
(same operation as if user clicked Ctrl+C).
How to program that? Is it possible?


J French

2004-03-28, 9:58 pm

On Fri, 26 Mar 2004 22:18:33 -0500, "Jack" <replyto@newsgroup> wrote:

>Hello,
> Here is the scenario:
>1. My app is able to accept hotkey: let say Alt + D and is sitting in the
>background.
>2. User opens some text document, highlight the text and hits Alt+D
>3. My app receives Alt+D and now it wants to put selected text to Clipboard
>(same operation as if user clicked Ctrl+C).
>How to program that? Is it possible?


RegisterHotKey is what you need

There is a good example in the downloadable API Guide from
www.AllAPI.net
Jack

2004-03-28, 9:58 pm

Your answer does not have merit.
As I said my app is able to receive Alt+D as a hotkey. It means hotkey is
registered already.
What I do not know is the other part of my question.
How my app can put on clipboard (or read it) the text which was highlighted
by another app.
Is it possible at all, and if it is how to approach that problem?
Jack

"J French" <erewhon@nowhere.com> wrote in message
news:40653c7d.348156999@news.btclick.com...
> On Fri, 26 Mar 2004 22:18:33 -0500, "Jack" <replyto@newsgroup> wrote:
>
Clipboard[color=darkred]
>
> RegisterHotKey is what you need
>
> There is a good example in the downloadable API Guide from
> www.AllAPI.net



J French

2004-03-28, 9:59 pm

On Sat, 27 Mar 2004 10:14:56 -0500, "Jack" <replyto@newsgroup> wrote:

>Your answer does not have merit.
>As I said my app is able to receive Alt+D as a hotkey. It means hotkey is
>registered already.
>What I do not know is the other part of my question.
>How my app can put on clipboard (or read it) the text which was highlighted
>by another app.
>Is it possible at all, and if it is how to approach that problem?


It was not clear that you had already used RegisterHotKey

The next step is so obvious, that I assumed that you had not reached
stage 1

You don't put the selected text on the Clipboard
- you /tell/ the App which the User is working on to put it on the
Clipboard
- ie: you force it a Ctl C
Since that App already has system focus, Keybd_Event is what you need

Here is a little snooper that does what you want, but uses a Timer
instead of a Hot Key :-

Option Explicit

'
' Snoop Marked Text
' using Clipboard
'
' J French 17/9/02
'
' Add one Timer
'
' Hacked from KPD code

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Declare Function GetCursorPos _
Lib "user32" _
(lpPoint As POINTAPI) As Long


Private Declare Sub SetWindowPos _
Lib "user32" _
(ByVal hWnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long)

Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40

Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)

Private Const VK_CONTROL = &H11
Private Const VK_C = &H43
Private Const KEYEVENTF_KEYUP = &H2


Private Sub Form_Load()
Me.Cls
Timer1.Enabled = True
Timer1.Interval = 250
Clipboard.Clear
End Sub


Private Sub Form_Paint()
Me.CurrentX = 0
Me.CurrentY = 0
Me.Print "Highlight Text in Another App"
End Sub

Private Sub Timer1_Timer()
Call SnapText
End Sub

Private Sub SnapText()
Dim Pt As POINTAPI
Dim S$
Static H_Pt As POINTAPI
Static Count&

'Get the current cursor position
Call GetCursorPos(Pt)
If H_Pt.X = Pt.X Then
If H_Pt.Y = Pt.Y Then
If Count > 2 Then
Exit Sub
End If
End If
End If

' --- Remember the Curser Position
H_Pt = Pt

' --- make sure we are visible
SetWindowPos Me.hWnd, HWND_TOPMOST, _
0, 0, 0, 0, _
SWP_NOACTIVATE _
Or SWP_SHOWWINDOW _
Or SWP_NOMOVE _
Or SWP_NOSIZE

' --- Force in Ctl C
Call keybd_event(VK_CONTROL, 0, 0, 0)
Call keybd_event(VK_C, 0, 0, 0)
Call keybd_event(VK_C, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)

' --- Read the Keyboard - it may be 'locked'
On Error Resume Next
S$ = Clipboard.GetText
If Err Then Exit Sub
On Error GoTo 0
Call LS_ShowText(S$, Count)

End Sub

Private Sub LS_ShowText(S$, Count&)
Static H_S$

' --- Changed Data
If S$ <> H_S$ Then
H_S$ = S$
Count = 0
Exit Sub
End If
' --- It has remained unchanged
If Count < 9 Then
Count = Count + 1
End If
' --- Display 'settled' data
If Count = 2 Then
Me.Print S$
End If
End Sub


Jack

2004-03-28, 9:59 pm

Thanks a lot. I needed that.
I've completely forgot about keybd_event :)
But now, I have a problem when trying your code.
When executing:
Call keybd_event(VK_CONTROL, 0, 0, 0) ---> it returns from this call
Call keybd_event(VK_C, 0, 0, 0) -----> it never returns from this call
I am using Windows 2000 and vbasic5 if that matters.

My complete relevant code looks like that:

Public Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long

Const WM_HOTKEY = &H312

Select Case uMsg

Case WM_HOTKEY
If oHotKey = 1 Then
Const VK_CONTROL = &H11
Const VK_C = &H43
Const KEYEVENTF_KEYUP = &H2
Debug.Print "Hotkey: " & wParam
Call keybd_event(VK_CONTROL, 0, 0, 0)
Call keybd_event(VK_C, 0, 0, 0) '-----------> never
returns from this call
Call keybd_event(VK_C, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
Call ProcessClipboard
End If
End Select
error_WindowProc:
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Function

"J French" <erewhon@nowhere.com> wrote in message
news:4066999c.7743240@news.btclick.com...
> On Sat, 27 Mar 2004 10:14:56 -0500, "Jack" <replyto@newsgroup> wrote:
>
highlighted[color=darkred]
>
> It was not clear that you had already used RegisterHotKey
>
> The next step is so obvious, that I assumed that you had not reached
> stage 1
>
> You don't put the selected text on the Clipboard
> - you /tell/ the App which the User is working on to put it on the
> Clipboard
> - ie: you force it a Ctl C
> Since that App already has system focus, Keybd_Event is what you need
>
> Here is a little snooper that does what you want, but uses a Timer
> instead of a Hot Key :-
>
> Option Explicit
>
> '
> ' Snoop Marked Text
> ' using Clipboard
> '
> ' J French 17/9/02
> '
> ' Add one Timer
> '
> ' Hacked from KPD code
>
> Private Type POINTAPI
> X As Long
> Y As Long
> End Type
>
> Private Declare Function GetCursorPos _
> Lib "user32" _
> (lpPoint As POINTAPI) As Long
>
>
> Private Declare Sub SetWindowPos _
> Lib "user32" _
> (ByVal hWnd As Long, _
> ByVal hWndInsertAfter As Long, _
> ByVal X As Long, _
> ByVal Y As Long, _
> ByVal cx As Long, _
> ByVal cy As Long, _
> ByVal wFlags As Long)
>
> Private Const HWND_TOPMOST = -1
> Private Const HWND_NOTOPMOST = -2
> Private Const SWP_NOSIZE = &H1
> Private Const SWP_NOMOVE = &H2
> Private Const SWP_NOACTIVATE = &H10
> Private Const SWP_SHOWWINDOW = &H40
>
> Private Declare Sub keybd_event Lib "user32" _
> (ByVal bVk As Byte, _
> ByVal bScan As Byte, _
> ByVal dwFlags As Long, _
> ByVal dwExtraInfo As Long)
>
> Private Const VK_CONTROL = &H11
> Private Const VK_C = &H43
> Private Const KEYEVENTF_KEYUP = &H2
>
>
> Private Sub Form_Load()
> Me.Cls
> Timer1.Enabled = True
> Timer1.Interval = 250
> Clipboard.Clear
> End Sub
>
>
> Private Sub Form_Paint()
> Me.CurrentX = 0
> Me.CurrentY = 0
> Me.Print "Highlight Text in Another App"
> End Sub
>
> Private Sub Timer1_Timer()
> Call SnapText
> End Sub
>
> Private Sub SnapText()
> Dim Pt As POINTAPI
> Dim S$
> Static H_Pt As POINTAPI
> Static Count&
>
> 'Get the current cursor position
> Call GetCursorPos(Pt)
> If H_Pt.X = Pt.X Then
> If H_Pt.Y = Pt.Y Then
> If Count > 2 Then
> Exit Sub
> End If
> End If
> End If
>
> ' --- Remember the Curser Position
> H_Pt = Pt
>
> ' --- make sure we are visible
> SetWindowPos Me.hWnd, HWND_TOPMOST, _
> 0, 0, 0, 0, _
> SWP_NOACTIVATE _
> Or SWP_SHOWWINDOW _
> Or SWP_NOMOVE _
> Or SWP_NOSIZE
>
> ' --- Force in Ctl C
> Call keybd_event(VK_CONTROL, 0, 0, 0)
> Call keybd_event(VK_C, 0, 0, 0)
> Call keybd_event(VK_C, 0, KEYEVENTF_KEYUP, 0)
> Call keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
>
> ' --- Read the Keyboard - it may be 'locked'
> On Error Resume Next
> S$ = Clipboard.GetText
> If Err Then Exit Sub
> On Error GoTo 0
> Call LS_ShowText(S$, Count)
>
> End Sub
>
> Private Sub LS_ShowText(S$, Count&)
> Static H_S$
>
> ' --- Changed Data
> If S$ <> H_S$ Then
> H_S$ = S$
> Count = 0
> Exit Sub
> End If
> ' --- It has remained unchanged
> If Count < 9 Then
> Count = Count + 1
> End If
> ' --- Display 'settled' data
> If Count = 2 Then
> Me.Print S$
> End If
> End Sub
>
>



J French

2004-03-28, 9:59 pm

On Sun, 28 Mar 2004 11:14:49 -0500, "Jack" <replyto@newsgroup> wrote:

>Thanks a lot. I needed that.
>I've completely forgot about keybd_event :)
>But now, I have a problem when trying your code.
>When executing:
> Call keybd_event(VK_CONTROL, 0, 0, 0) ---> it returns from this call
> Call keybd_event(VK_C, 0, 0, 0) -----> it never returns from this call
>I am using Windows 2000 and vbasic5 if that matters.
>
>My complete relevant code looks like that:
>
>Public Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal
>wParam As Long, ByVal lParam As Long) As Long


Christ ! Don't get the Hot Key like that !
Doing things like that inside a Windows Hook is lethal
- you need a bit of protection

We both know that the routine works inside a VB Timer Event, so just
Enable the Timer inside your WinHook

>
>Const WM_HOTKEY = &H312
>
>Select Case uMsg
>
> Case WM_HOTKEY
> If oHotKey = 1 Then
> Const VK_CONTROL = &H11
> Const VK_C = &H43
> Const KEYEVENTF_KEYUP = &H2
> Debug.Print "Hotkey: " & wParam
> Call keybd_event(VK_CONTROL, 0, 0, 0)
> Call keybd_event(VK_C, 0, 0, 0) '-----------> never
>returns from this call
> Call keybd_event(VK_C, 0, KEYEVENTF_KEYUP, 0)
> Call keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
> Call ProcessClipboard
> End If
>End Select
>error_WindowProc:
> WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
>End Function


Jack

2004-03-28, 9:59 pm

I have changed that as you advised:
Case WM_HOTKEY
If oHotKey = 1 And wParam = Left(sHotKey, InStr(1, sHotKey, ",") -
1) Then
fCopy = 2
frmDialEngine.tmrCopy.Enabled = True
End If

but I have located the problem somewhere else. Ctrl +C was used by another
application and that's why 'C' was hijacked by Windows.
Thanks,
Jack

"J French" <erewhon@nowhere.com> wrote in message
news:406723f4.26641964@news.btclick.com...
> On Sun, 28 Mar 2004 11:14:49 -0500, "Jack" <replyto@newsgroup> wrote:
>
call[color=darkred]
>
> Christ ! Don't get the Hot Key like that !
> Doing things like that inside a Windows Hook is lethal
> - you need a bit of protection
>
> We both know that the routine works inside a VB Timer Event, so just
> Enable the Timer inside your WinHook
>
>



Jack

2004-03-28, 11:30 pm

I run into some obstacle and I do not know how to solve that.
1. By trial I've found that my hotkey can be only Ctrl+(A-Z) --->except
some common Windows shortcuts like Ctrl+X, Ctrl+O etc.
Selecting anything else causes virtual VK_CONTROL and VK_C not working
(Clipboard gets nothing)
2. The first attempt to get new clipboard content fails with Run-Time Eror
521: "Can't open Clipboard
There is not any other app using clipboard at that moment. It looks like my
own app prevents opening the clipboard again. I have noticed when using a
debugger that after repeating immediately the same line of code there is not
error any more as if that debugger acknowledgment of error clears the way to
open the clipboard again.
So, to avoid that error I coded the following way:

On Error Resume Next
lname = Clipboard.GetText
lname = Clipboard.GetText 'specially doubled line to avoid run-time
error 521

This solves the immediate problem but I do not think it is a right approach
to it.

After more thinking and experimenting I came to the conclusion that the
error is caused by the fact that the keyboard keys are physically pressed
down (hotkey) at the moment when virtual keys are being called Up and that
KEYEVENTF_KEYUP may just not work. But I cannot prove it. I put some time
delay so I could give time to release keyboard keys before calling virtual
Ctrl+C but the error still was in there.
I am very much interested in your opinion about that error and solving it
another way.
Also, is it possible for me to use another hotkey key combination, or am I
stuck with Ctrl+(A-Z) only?
Thanks,
Jack

"J French" <erewhon@nowhere.com> wrote in message
news:406723f4.26641964@news.btclick.com...
> On Sun, 28 Mar 2004 11:14:49 -0500, "Jack" <replyto@newsgroup> wrote:
>
call[color=darkred]
>
> Christ ! Don't get the Hot Key like that !
> Doing things like that inside a Windows Hook is lethal
> - you need a bit of protection
>
> We both know that the routine works inside a VB Timer Event, so just
> Enable the Timer inside your WinHook
>
>



J French

2004-03-29, 7:30 am

I've been testing things while writing this, it is more a log of my
findings - but should, I hope be useful.

On Sun, 28 Mar 2004 21:38:00 -0500, "Jack" <replyto@newsgroup> wrote:

>I run into some obstacle and I do not know how to solve that.
>1. By trial I've found that my hotkey can be only Ctrl+(A-Z) --->except
>some common Windows shortcuts like Ctrl+X, Ctrl+O etc.
> Selecting anything else causes virtual VK_CONTROL and VK_C not working
>(Clipboard gets nothing)


I've looked at that, and you are right, as you later guess, because
those other keys like Alt are still 'down'
- so the App is getting Ctl Alt C

You could try sending it a Key Up for the Alt or Shift Key

Alt is VK_MENU is &h12
VK_SHIFT is &h10

>2. The first attempt to get new clipboard content fails with Run-Time Eror
>521: "Can't open Clipboard


When you start your App, try Clipboard.Clear

>There is not any other app using clipboard at that moment. It looks like my
>own app prevents opening the clipboard again. I have noticed when using a
>debugger that after repeating immediately the same line of code there is not
>error any more as if that debugger acknowledgment of error clears the way to
>open the clipboard again.
>So, to avoid that error I coded the following way:
>
> On Error Resume Next
> lname = Clipboard.GetText
> lname = Clipboard.GetText 'specially doubled line to avoid run-time
>error 521
>
>This solves the immediate problem but I do not think it is a right approach
>to it.


It looks ugly, but I guess we are meddling with stuff that confuses
Windows

>
>After more thinking and experimenting I came to the conclusion that the
>error is caused by the fact that the keyboard keys are physically pressed
>down (hotkey) at the moment when virtual keys are being called Up and that
>KEYEVENTF_KEYUP may just not work. But I cannot prove it. I put some time
>delay so I could give time to release keyboard keys before calling virtual
>Ctrl+C but the error still was in there.


I think you are absolutely right
Have you tried forcing in a Key Up for the Alt ?
- no - I just tried it - no dice - Alt activates the menu

I have got it working reliably with Ctl Z

- Ctl F over the VB IDE gave me ureliable results
.... until I put this after the Keybd_Events

' --- Give up Timer Slice
DoEvents
Sleep 250

That kind of figures - one needs to give the other App a chance to do
its stuff before grabbing the Clipboard data

I also have it working with Ctl + vbKeyF12

>I am very much interested in your opinion about that error and solving it
>another way.


Have you looked at the SetClipboardViewer API ?
The www.AllAPI.net Guide has a good example
- that way you can leach off Ctl C

>Also, is it possible for me to use another hotkey key combination, or am I
>stuck with Ctrl+(A-Z) only?


I think the only unsafe keys are the Alt ones
- because the Alt activates the other App's Menu, which makes things
unreliable

- I also tried Ctl F1 - not a widely used combination - no problem

Amusingly hijacking F1 works fine as well
- nobody using your utility would want the Help to pop up

I did not have any Clipboard errors, but I did find it slightly
unreliable until I put in the : DoEvents and Sleep 250

I'm not entirely sure what you are aiming for, and the choice of
solution depends on your objectives.
However, I'm pretty sure that you can make something that does what
you want using one of these approaches.

I'll keep an eye on this thread - it's very interesting


Jack

2004-03-29, 11:30 am

One thing to clarify about that error I am having.
You misread my post. It does not happen on program startup.
It happens on each (first) attempt to get new clipboard data.
The next consecutive attempt to retrieve the clipboard returns no error.
For sure the error has something to do with the virtual keys sent.
Here is proof:
from my Win hook routine:
=============================
Case WM_DRAWCLIPBOARD
Call ProcessClipboard
Case WM_HOTKEY
If oHotKey = 1 And wParam = Left(sHotKey, InStr(1, sHotKey, ",") -
1) Then
fCopy = 2
Debug.Print "Hotkey: " & wParam
frmDialEngine.tmrCopy.Enabled = True
End If
==================================

ProcessClipboard routine retieves Clipboard data.
When it is being called from Case WM_DRAWCLIPBOARD there is not error 521.
When it is being called from Case WM_HOTKEY (it is being called from
timerCopy routine after sending virtual keys) there is an error 521.

I wonder why you are not having that error. Maybe it is OS related? Iam
using Win2000 SP3 and vbasic5 SP3
Can you really send virtual key 'C' and at the same time hold down Ctrl on
your keyboard and then call Clipboard.GetText without an error? Please
confirm that.
Thanks,
Jack

"J French" <erewhon@nowhere.com> wrote in message
news:4067f525.5535381@news.btclick.com...
> I've been testing things while writing this, it is more a log of my
> findings - but should, I hope be useful.
>
> On Sun, 28 Mar 2004 21:38:00 -0500, "Jack" <replyto@newsgroup> wrote:
>
working[color=darkred]
>
> I've looked at that, and you are right, as you later guess, because
> those other keys like Alt are still 'down'
> - so the App is getting Ctl Alt C
>
> You could try sending it a Key Up for the Alt or Shift Key
>
> Alt is VK_MENU is &h12
> VK_SHIFT is &h10
>
Eror[color=darkred]
>
> When you start your App, try Clipboard.Clear
>
my[color=darkred]
not[color=darkred]
to[color=darkred]
run-time[color=darkred]
approach[color=darkred]
>
> It looks ugly, but I guess we are meddling with stuff that confuses
> Windows
>
that[color=darkred]
virtual[color=darkred]
>
> I think you are absolutely right
> Have you tried forcing in a Key Up for the Alt ?
> - no - I just tried it - no dice - Alt activates the menu
>
> I have got it working reliably with Ctl Z
>
> - Ctl F over the VB IDE gave me ureliable results
> ... until I put this after the Keybd_Events
>
> ' --- Give up Timer Slice
> DoEvents
> Sleep 250
>
> That kind of figures - one needs to give the other App a chance to do
> its stuff before grabbing the Clipboard data
>
> I also have it working with Ctl + vbKeyF12
>
>
> Have you looked at the SetClipboardViewer API ?
> The www.AllAPI.net Guide has a good example
> - that way you can leach off Ctl C
>
I[color=darkred]
>
> I think the only unsafe keys are the Alt ones
> - because the Alt activates the other App's Menu, which makes things
> unreliable
>
> - I also tried Ctl F1 - not a widely used combination - no problem
>
> Amusingly hijacking F1 works fine as well
> - nobody using your utility would want the Help to pop up
>
> I did not have any Clipboard errors, but I did find it slightly
> unreliable until I put in the : DoEvents and Sleep 250
>
> I'm not entirely sure what you are aiming for, and the choice of
> solution depends on your objectives.
> However, I'm pretty sure that you can make something that does what
> you want using one of these approaches.
>
> I'll keep an eye on this thread - it's very interesting
>
>



J French

2004-03-29, 12:30 pm

On Mon, 29 Mar 2004 10:39:07 -0500, "Jack" <replyto@newsgroup> wrote:

>One thing to clarify about that error I am having.
>You misread my post. It does not happen on program startup.
>It happens on each (first) attempt to get new clipboard data.
>The next consecutive attempt to retrieve the clipboard returns no error.
>For sure the error has something to do with the virtual keys sent.
>Here is proof:
>from my Win hook routine:
>=============================
> Case WM_DRAWCLIPBOARD
> Call ProcessClipboard
> Case WM_HOTKEY
> If oHotKey = 1 And wParam = Left(sHotKey, InStr(1, sHotKey, ",") -
>1) Then
> fCopy = 2
> Debug.Print "Hotkey: " & wParam
> frmDialEngine.tmrCopy.Enabled = True
> End If
>==================================
>
>ProcessClipboard routine retieves Clipboard data.
>When it is being called from Case WM_DRAWCLIPBOARD there is not error 521.
>When it is being called from Case WM_HOTKEY (it is being called from
>timerCopy routine after sending virtual keys) there is an error 521.
>
>I wonder why you are not having that error. Maybe it is OS related? Iam
>using Win2000 SP3 and vbasic5 SP3
>Can you really send virtual key 'C' and at the same time hold down Ctrl on
>your keyboard and then call Clipboard.GetText without an error? Please
>confirm that.


No - I tried that - not while HotKeying Ctl C - no chance
I expected a spectacular crash - but it just did not work
- fortunately not what I expected

I've been thinking about this a fair bit, I am sure that it is a
matter of synchronization.

Did you try the DoEvents (important) followed by the Sleep 250

That is for tomorrow
Jack

2004-03-30, 2:30 am

Interesting.
I do not have Sleep function in my vbasic5 and I just put some time delay
loop to no avail.
But, when I use DoEvents just before getting clipboard data there is not
error anymore :)
I do this:
DoEvents
lname = Clipboard.GetText
How about that?
Jack
"J French" <erewhon@nowhere.com> wrote in message
news:40685007.28804761@news.btclick.com...
> On Mon, 29 Mar 2004 10:39:07 -0500, "Jack" <replyto@newsgroup> wrote:
>
",") -[color=darkred]
521.[color=darkred]
on[color=darkred]
>
> No - I tried that - not while HotKeying Ctl C - no chance
> I expected a spectacular crash - but it just did not work
> - fortunately not what I expected
>
> I've been thinking about this a fair bit, I am sure that it is a
> matter of synchronization.
>
> Did you try the DoEvents (important) followed by the Sleep 250
>
> That is for tomorrow



J French

2004-03-30, 3:30 am

On Tue, 30 Mar 2004 01:15:35 -0500, "Jack" <replyto@newsgroup> wrote:

>Interesting.
>I do not have Sleep function in my vbasic5 and I just put some time delay
>loop to no avail.


You'll hav it in a minute

>But, when I use DoEvents just before getting clipboard data there is not
>error anymore :)


Great

>I do this:
> DoEvents
> lname = Clipboard.GetText
>How about that?


That looks fine.

Now you are in for a shock.

VB sits on top of Windows and most of what it really does is make
Calls into Subs and Functions that Windows provides.
- these are called APIs - Application Programmer Interfaces

There are hundreds, or rather thousands of APIs

Here is one of them
- stick it at the top of your Form and presto
.... you have Sleep( 250 ) in VB5

Private Declare Sub Sleep _
Lib "kernel32.dll" _
(ByVal dwMilliseconds As Long)

There is a superb downloadable API Guide at:
www.AllAPI.net

It is full of easy to test examples

Easily the most useful programming resource I have ever found
( although it is best used in conjunction with the Win32 Programmer's
Reference Help Files and MSDN )

I recommend that you put Sleep(250) in after the DoEvents because
DoEvents processes all /your/ outstanding Windows Messages in /your/
message queue
- but Sleep(250) hand over the CPU to the other Apps running on the
system for 1/4 of a second, which gives the App that you are pumping
key presses at a chance to do its Clipboard copy and close the
Clipboard
- probably just belt and braces, but still good practice

I rather suspect that you will have some fun with the API Guide

Good Luck
Jack

2004-03-30, 11:30 am

Hey, thanks a lot. It is very nice to talk with you.
BTW, I know about API's and about API Guide > www.AllAPI.net.
In fact I am using that API guide already for 3 years.
When talking about Sleep I assumed that it is the same kind of function as
for example Replace, which exists in vbasic6 but it does not in vbasic5. You
will not find Replace in the API Guide.
I hate vbasic6 from the very beginning when I bought it. It is a bloatware
for me.
Vbasic6 library is huge, making your program unecessary big, Setup Wizard is
screwed up and I timed some functions executions and on average it was 7-10
times slower then in vbasic5.
I think I will stick with vbasic5.
Thanks,
Jack

"J French" <erewhon@nowhere.com> wrote in message
news:40691ba0.80933856@news.btclick.com...
> On Tue, 30 Mar 2004 01:15:35 -0500, "Jack" <replyto@newsgroup> wrote:
>
>
> You'll hav it in a minute
>
>
> Great
>
>
> That looks fine.
>
> Now you are in for a shock.
>
> VB sits on top of Windows and most of what it really does is make
> Calls into Subs and Functions that Windows provides.
> - these are called APIs - Application Programmer Interfaces
>
> There are hundreds, or rather thousands of APIs
>
> Here is one of them
> - stick it at the top of your Form and presto
> ... you have Sleep( 250 ) in VB5
>
> Private Declare Sub Sleep _
> Lib "kernel32.dll" _
> (ByVal dwMilliseconds As Long)
>
> There is a superb downloadable API Guide at:
> www.AllAPI.net
>
> It is full of easy to test examples
>
> Easily the most useful programming resource I have ever found
> ( although it is best used in conjunction with the Win32 Programmer's
> Reference Help Files and MSDN )
>
> I recommend that you put Sleep(250) in after the DoEvents because
> DoEvents processes all /your/ outstanding Windows Messages in /your/
> message queue
> - but Sleep(250) hand over the CPU to the other Apps running on the
> system for 1/4 of a second, which gives the App that you are pumping
> key presses at a chance to do its Clipboard copy and close the
> Clipboard
> - probably just belt and braces, but still good practice
>
> I rather suspect that you will have some fun with the API Guide
>
> Good Luck



Bob Butler

2004-03-30, 11:30 am

"Jack" <replyto@newsgroup> wrote in message
news:uvPFNhmFEHA.1368@TK2MSFTNGP11.phx.gbl
> Hey, thanks a lot. It is very nice to talk with you.
> BTW, I know about API's and about API Guide > www.AllAPI.net.
> In fact I am using that API guide already for 3 years.
> When talking about Sleep I assumed that it is the same kind of
> function as for example Replace, which exists in vbasic6 but it does
> not in vbasic5. You will not find Replace in the API Guide.
> I hate vbasic6 from the very beginning when I bought it. It is a
> bloatware for me.
> Vbasic6 library is huge, making your program unecessary big, Setup
> Wizard is screwed up and I timed some functions executions and on
> average it was 7-10 times slower then in vbasic5.
> I think I will stick with vbasic5.


I have never found any siginificant difference between VB5 and VB6; I go
back and forth between them all the time. The runtime libraries aren't that
much different in size or performance so maybe you had something else wrong
in your setup. If you remember what functions were 7-10 times slower please
give some details.

--
Reply to the group so all can participate
VB.Net... just say "No"

Jack

2004-03-30, 11:30 am

I read about that Sleep function and I am a little .
In my program (telephony application) I am using quite often the time delays
loops.
The time delay function I am using looks like that:

Public Start as Single
Public Sub DelayTimer(TimeDelay As Single)
Start = Timer ' Set start time.
Do While Timer < Start + TimeDelay
Loop
End Sub

and 'interactive' time delay function:

Public Sub DelayTimerInter(TimeDelay As Single)
Start = Timer ' Set start time.
Do While Timer < Start + TimeDelay
DoEvents
Loop
End Sub

How these above compare with the Sleep function? What is better to use?
I am not a professional programmer but I like to do things the right way :)
Jack

"J French" <erewhon@nowhere.com> wrote in message
news:40691ba0.80933856@news.btclick.com...
> On Tue, 30 Mar 2004 01:15:35 -0500, "Jack" <replyto@newsgroup> wrote:
>
>
> You'll hav it in a minute
>
>
> Great
>
>
> That looks fine.
>
> Now you are in for a shock.
>
> VB sits on top of Windows and most of what it really does is make
> Calls into Subs and Functions that Windows provides.
> - these are called APIs - Application Programmer Interfaces
>
> There are hundreds, or rather thousands of APIs
>
> Here is one of them
> - stick it at the top of your Form and presto
> ... you have Sleep( 250 ) in VB5
>
> Private Declare Sub Sleep _
> Lib "kernel32.dll" _
> (ByVal dwMilliseconds As Long)
>
> There is a superb downloadable API Guide at:
> www.AllAPI.net
>
> It is full of easy to test examples
>
> Easily the most useful programming resource I have ever found
> ( although it is best used in conjunction with the Win32 Programmer's
> Reference Help Files and MSDN )
>
> I recommend that you put Sleep(250) in after the DoEvents because
> DoEvents processes all /your/ outstanding Windows Messages in /your/
> message queue
> - but Sleep(250) hand over the CPU to the other Apps running on the
> system for 1/4 of a second, which gives the App that you are pumping
> key presses at a chance to do its Clipboard copy and close the
> Clipboard
> - probably just belt and braces, but still good practice
>
> I rather suspect that you will have some fun with the API Guide
>
> Good Luck



Bob Butler

2004-03-30, 11:30 am

"Jack" <replyto@newsgroup> wrote in message
news:u2$o01mFEHA.1128@TK2MSFTNGP11.phx.gbl
> I read about that Sleep function and I am a little .
> In my program (telephony application) I am using quite often the time
> delays loops.
> The time delay function I am using looks like that:
>
> Public Start as Single
> Public Sub DelayTimer(TimeDelay As Single)
> Start = Timer ' Set start time.
> Do While Timer < Start + TimeDelay
> Loop
> End Sub


That locks up your app for the delay inteval; cpu usage will show 100% and
your app will be "not responding" if it goes long enough. If you have any
forms displayed the user will get no response from clicking or typing until
the loop completes and the screen will not be refreshed if you switch to
another app in the meantime.

Note that if the start time is just before midnight and the delay interval
takes you past midnight this can lock up and never exit since the Timer
function resets at midnight.

> and 'interactive' time delay function:
>
> Public Sub DelayTimerInter(TimeDelay As Single)
> Start = Timer ' Set start time.
> Do While Timer < Start + TimeDelay
> DoEvents
> Loop
> End Sub


That is similar to the first except that any user action with the keyboard
or mouse is processed and the task manager won't show your app 'not
responding' since you are yielding time with DoEvents. The CPU usage will
still be 100%.

> How these above compare with the Sleep function? What is better to
> use? I am not a professional programmer but I like to do things the
> right way :) Jack


Sleep is similar to your first function except that CPU usage is not pegged
to 100%. The Sleep API tells the syst6em to just ignore your app for the
specified period. The best solution IMO is to combine things:

Public Sub DelayTimerInter(ByVal DelaySeconds As Long)
Dim Target As Date
Target = DateAdd("s", DelaySeconds, Now)
Do Until Now >= Target
Sleep 1000 ' 1 second delay
DoEvents ' check for activity
Loop
End Sub


--
Reply to the group so all can participate
VB.Net... just say "No"

Jack

2004-03-30, 12:30 pm

I posted twice in this newsgroup my findings looking for answers (3-4 years
ago).
I did not have any responses.
I will try to search Google for it.
Jack
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:%23Kb1a1mFEHA.3056@TK2MSFTNGP12.phx.gbl...
> "Jack" <replyto@newsgroup> wrote in message
> news:uvPFNhmFEHA.1368@TK2MSFTNGP11.phx.gbl
>
> I have never found any siginificant difference between VB5 and VB6; I go
> back and forth between them all the time. The runtime libraries aren't

that
> much different in size or performance so maybe you had something else

wrong
> in your setup. If you remember what functions were 7-10 times slower

please
> give some details.
>
> --
> Reply to the group so all can participate
> VB.Net... just say "No"
>



J French

2004-03-30, 12:30 pm

On Tue, 30 Mar 2004 10:04:26 -0500, "Jack" <replyto@newsgroup> wrote:

>Hey, thanks a lot. It is very nice to talk with you.


At first I thought you were obnoxious
- I changed my mind

>BTW, I know about API's and about API Guide > www.AllAPI.net.
>In fact I am using that API guide already for 3 years.


A sign of senility is preaching to the converted
- it really is great stuff !

>When talking about Sleep I assumed that it is the same kind of function as
>for example Replace, which exists in vbasic6 but it does not in vbasic5. You
>will not find Replace in the API Guide.


Yes, nor Split() - I can live without them

>I hate vbasic6 from the very beginning when I bought it. It is a bloatware
>for me.
>Vbasic6 library is huge, making your program unecessary big, Setup Wizard is
>screwed up and I timed some functions executions and on average it was 7-10
>times slower then in vbasic5.


I loathe Setup Pissards - no OCXes - no problems

>I think I will stick with vbasic5.


Cover all bases - know your enemy

>Thanks,
> Jack


From an annoying jerk, you turned into a 'teleocraticic' programmer
- that is a compliment, but look it up anyway

Good Luck - and may you not need it

Regards - Jerry
J French

2004-03-30, 1:30 pm


1) you messed up your post (two identical examples)

2) Bob has warned you about Timer! (note the !)
Best be bulletproof though

3) WaitMessage - look into it
Sleep() is kind to other Apps, it sor of forces them to work
If you are waiting for a signal then ... WaitMessage

On Tue, 30 Mar 2004 10:41:20 -0500, "Jack" <replyto@newsgroup> wrote:

>I read about that Sleep function and I am a little .
>In my program (telephony application) I am using quite often the time delays
>loops.
>The time delay function I am using looks like that:
>
>Public Start as Single
>Public Sub DelayTimer(TimeDelay As Single)
> Start = Timer ' Set start time.
> Do While Timer < Start + TimeDelay
> Loop
>End Sub
>
>and 'interactive' time delay function:
>
>Public Sub DelayTimerInter(TimeDelay As Single)
> Start = Timer ' Set start time.
> Do While Timer < Start + TimeDelay
> DoEvents
> Loop
>End Sub
>
>How these above compare with the Sleep function? What is better to use?
>I am not a professional programmer but I like to do things the right way :)
>Jack
>
>"J French" <erewhon@nowhere.com> wrote in message
>news:40691ba0.80933856@news.btclick.com...
>
>


Rick Rothstein

2004-03-30, 1:30 pm

> >When talking about Sleep I assumed that it is the same kind of function
as
You[color=darkred]
>
> Yes, nor Split() - I can live without them


Live without Replace or Split????

<sputter>....<sputter>....Blasphemy!!!

Rick


Bob Butler

2004-03-30, 1:30 pm

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:O$0dM1nFEHA.2664@TK2MSFTNGP11.phx.gbl
>
> Live without Replace or Split????
>
> <sputter>....<sputter>....Blasphemy!!!


Replace, no... Split and Join, easily! <g>

--
Reply to the group so all can participate
VB.Net... just say "No"

Jack

2004-03-30, 1:30 pm

I cannot find that post.
I remember that it involved populating ListView with the long list of items.
I did experiment under the same conditions:
1. the same computer
2. the same code
3. the same # of items to load
In vbasic6 it took over a 1 minute, in vbasic5 only 15 seconds.
Since then I reverted back to vb5.
Jack
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:%23Kb1a1mFEHA.3056@TK2MSFTNGP12.phx.gbl...
> "Jack" <replyto@newsgroup> wrote in message
> news:uvPFNhmFEHA.1368@TK2MSFTNGP11.phx.gbl
>
> I have never found any siginificant difference between VB5 and VB6; I go
> back and forth between them all the time. The runtime libraries aren't

that
> much different in size or performance so maybe you had something else

wrong
> in your setup. If you remember what functions were 7-10 times slower

please
> give some details.
>
> --
> Reply to the group so all can participate
> VB.Net... just say "No"
>



Bob Butler

2004-03-30, 1:30 pm

"Jack" <replyto@newsgroup> wrote in message
news:eZN3M%23nFEHA.700@TK2MSFTNGP09.phx.gbl
> I cannot find that post.
> I remember that it involved populating ListView with the long list of
> items. I did experiment under the same conditions:
> 1. the same computer
> 2. the same code
> 3. the same # of items to load
> In vbasic6 it took over a 1 minute, in vbasic5 only 15 seconds.
> Since then I reverted back to vb5.


I just tried the following in VB6 and with 10000 (which is about 9800 more
than I'd ever put in a listview) items it took less than a second to
populate

Option Explicit

Private Sub Form_Click()
Dim oItem As ListItem
Dim dStart As Date
Dim x As Long
dStart = Now
ListView1.ListItems.Clear
For x = 1 To 10000
Set oItem = ListView1.ListItems.Add(, , "Item " & CStr(x)) '
oItem.SubItems(1) = "xyzzy"
oItem.SubItems(2) = "plugh"
Next
Me.Caption = DateDiff("s", dStart, Now)
End Sub

Private Sub Form_Load()
Dim oHeader As ColumnHeader
ListView1.HideSelection = False
ListView1.View = lvwReport
Set oHeader = ListView1.ColumnHeaders.Add(, , "Item")
Set oHeader = ListView1.ColumnHeaders.Add(, , "sub item1")
Set oHeader = ListView1.ColumnHeaders.Add(, , "sub item2")
End Sub


--
Reply to the group so all can participate
VB.Net... just say "No"

Jack

2004-03-30, 3:30 pm

Bob, please keep in mind it was before Pentium computers!!!!

"Bob Butler" <tiredofit@nospam.com> wrote in message
news:%23k9ooFoFEHA.688@tk2msftngp13.phx.gbl...
> "Jack" <replyto@newsgroup> wrote in message
> news:eZN3M%23nFEHA.700@TK2MSFTNGP09.phx.gbl
>
> I just tried the following in VB6 and with 10000 (which is about 9800 more
> than I'd ever put in a listview) items it took less than a second to
> populate
>
> Option Explicit
>
> Private Sub Form_Click()
> Dim oItem As ListItem
> Dim dStart As Date
> Dim x As Long
> dStart = Now
> ListView1.ListItems.Clear
> For x = 1 To 10000
> Set oItem = ListView1.ListItems.Add(, , "Item " & CStr(x)) '
> oItem.SubItems(1) = "xyzzy"
> oItem.SubItems(2) = "plugh"
> Next
> Me.Caption = DateDiff("s", dStart, Now)
> End Sub
>
> Private Sub Form_Load()
> Dim oHeader As ColumnHeader
> ListView1.HideSelection = False
> ListView1.View = lvwReport
> Set oHeader = ListView1.ColumnHeaders.Add(, , "Item")
> Set oHeader = ListView1.ColumnHeaders.Add(, , "sub item1")
> Set oHeader = ListView1.ColumnHeaders.Add(, , "sub item2")
> End Sub
>
>
> --
> Reply to the group so all can participate
> VB.Net... just say "No"
>



Bob O`Bob

2004-03-30, 3:30 pm

Jack wrote:
>
> Bob, please keep in mind it was before Pentium computers!!!!


Maybe for you.
I had a P-II before VB5 even shipped.

Not that I fault anyone for holding onto old hardware as long as it works.
(I still have that P-II ... in fact I'm using it to post this)


Bob
--
Bob O`Bob

2004-03-30, 4:30 pm

Bob Butler wrote:
> "Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote
>
> Replace, no... Split and Join, easily! <g>
>


Not that hard to code VB5 replacements.
Probably could find several with google and codehound.



Bob
Bob Butler

2004-03-30, 6:30 pm

"Jack" <replyto@newsgroup> wrote in message
news:%23yP3v5oFEHA.3252@TK2MSFTNGP11.phx.gbl
> Bob, please keep in mind it was before Pentium computers!!!!


but then the VB5 would have been equally handicapped; I suspect it was
either an issue in an earlier version of the listview (pre-SP4 anyway) or
something else in the code being used at the time.

--
Reply to the group so all can participate
VB.Net... just say "No"

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com