Closed Thread
Results 1 to 6 of 6

Thread: caption problem

  1. #1
    ikkeugh is offline Learning Programmer
    Join Date
    Sep 2007
    Posts
    79
    Rep Power
    0

    caption problem

    Well , I'm having some trouble with a timer :
    The meaning of this program is that when I first press down the ctrl button , the captions of label 1 and 2 should come in labels 29 and 30 ,
    the second time I press ctrl , it should also come in labels 31 and 32. The third time I press ctrl , the captions should be - .

    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Form_Load()
    Timer2.Interval = 1
    End Sub
    
    Private Sub Timer2_Timer()
    On Error Resume Next
    If GetAsyncKeyState(17) Then
    Key = True
    End If
    
            If Label29.Caption = "-" And Key = True Then
                Label29.Caption = Label1.Caption
                Label30.Caption = Label2.Caption
                Key = False
            End If
            If Label29.Caption <> "-" And Label31.Caption = "-" And Key = True Then
                Label31.Caption = Label1.Caption
                Label32.Caption = Label2.Caption
                Key = False
            End If
            If Label31.Caption <> "-" And Label29.Caption <> "-" And Key = True Then
                Label29.Caption = "-"
                Label30.Caption = "-"
                Label31.Caption = "-"
                Label32.Caption = "-"
                Key = False
            End If
    
    End Sub
    The form contains : label1 , label 2 , label 29 , label 30 , label31 , label 32 and timer2

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30

    Use the Keyup/keypress event

    Where do you declare key? Also, why do you use a timer for a key press event? You should just capture the key even and then change the labels.

    [HIGHLIGHT="VB"]
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)

    If KeyCode = vbKeyControl then
    If Label29.Caption = "-" And Key = True Then
    Label29.Caption = Label1.Caption
    Label30.Caption = Label2.Caption
    Key = False
    End If
    If Label29.Caption <> "-" And Label31.Caption = "-" And Key = True Then
    Label31.Caption = Label1.Caption
    Label32.Caption = Label2.Caption
    Key = False
    End If
    If Label31.Caption <> "-" And Label29.Caption <> "-" And Key = True Then
    Label29.Caption = "-"
    Label30.Caption = "-"
    Label31.Caption = "-"
    Label32.Caption = "-"
    Key = False
    End If
    End Sub
    [/HIGHLIGHT]

  4. #3
    ikkeugh is offline Learning Programmer
    Join Date
    Sep 2007
    Posts
    79
    Rep Power
    0
    First of all : thanks !!!

    (second : you forgot an end if )

    Could you also make it so that even when the window isn't activated , when you push ctrl , it still does what it's supposed to do ?
    Last edited by ikkeugh; 10-15-2007 at 10:37 AM.

  5. #4
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30

    Capture keys in non-active window

    Whoops, I did.

    Capturing key strokes when the Window isn't active is a bit harder. You will need to use the GetAsyncKeyState to capture keys in a non-active window.

    Declare it:

    [highlight=vb]
    Private Declare Function GetAsyncKeyState Lib "User32" _
    (ByVal vKey As KeyCodeConstants) As Long
    [/highlight]


    Example Usage:
    [highlight=vb]
    Private Function KeyDown(ByVal vKey As KeyCodeConstants) _
    As Boolean
    KeyDown = GetAsyncKeyState(vKey) And &H8000
    If KeyDown(vbKeyControl) Then
    ` code Here
    end if
    End Sub
    [/highlight]

  6. #5
    ikkeugh is offline Learning Programmer
    Join Date
    Sep 2007
    Posts
    79
    Rep Power
    0
    That's not really working ...

    exemple
    Code:
    Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vKey As KeyCodeConstants) As Long
    Private Function KeyDown(ByVal vKey As KeyCodeConstants) As Boolean
    
    KeyDown = GetAsyncKeyState(vKey) And &H8000
    If KeyDown(vbKeyControl) Then
    Label1.Caption = "-"
    End If
    End Sub

  7. #6
    ikkeugh is offline Learning Programmer
    Join Date
    Sep 2007
    Posts
    79
    Rep Power
    0
    Isn't there an API for this ?
    For an example : getmouseposition also works when it isn't in an activated window .

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. If problem or cout problem?
    By chaoticape in forum C and C++
    Replies: 4
    Last Post: 06-10-2011, 10:29 AM
  2. C: Problem with solving problem
    By rakche in forum C and C++
    Replies: 15
    Last Post: 03-28-2010, 01:24 PM
  3. Change the caption of a window!!!
    By sakishrist in forum Visual Basic Programming
    Replies: 12
    Last Post: 05-04-2008, 06:03 AM
  4. Replies: 0
    Last Post: 04-26-2007, 05:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts