Announcement

Collapse
No announcement yet.

never lose focus

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • never lose focus

    anybody know how to make sure that my VBapp allways has the focus? I've tried this:

    sub form1_lostfocus
    me.setfocus
    end sub

    but that doesn't work, in fact the whole lostfocus function does not work, I tried
    sub form1_lostfocus
    msgbox "blabla"
    end sub

    and it never gave me a messagebox telling me to blabla...

    thanks!
    If at first you don't succeed.........
    destroy all evidence you tried

    for info on t6369c LCD or Presslab's powersupply check:
    http://www.namms.tk <=updated!!

  • #2
    I assume by "always have the focus" that you mean that you want your program to always be the topmost form. If that's what you're after, check out this link:

    http://216.26.168.92/tips/tip4.html

    I think there's another API call that you can make to do this, but I can't think of it off the top of my head.

    Comment


    • #3
      no that doesn't work, even if the app is allways the topmost app, that doesn't mean that it has the focus (is the current active form).

      Problem is that I use my keyboard to handle my app, but if it doesn't have the focus I'm handling another as well as my app.

      example:
      my app starts up winamp. if winamp has the focus and I press B winamp will go to the next songs & my app will do whatever I've programmed "under" B. I don't want winamp to respond to my key-touching...

      am I clear? (my english isn't all that...)
      If at first you don't succeed.........
      destroy all evidence you tried

      for info on t6369c LCD or Presslab's powersupply check:
      http://www.namms.tk <=updated!!

      Comment


      • #4
        Probably no help at all

        marsjell,
        I don't know VB but in C++, a neat trick is to get a handle to the external programs window proc (FindWindow etc.), save the handle away, replace the window proc handle with your own proc handle (SetWindowLong) and in the new proc pass any messages that you don't want to handle back to the original proc (CallWndProg).
        I used this as I couldn't get get SetWindowFocus to work.
        I don't know if you can do this lower level stuff in VB.
        ~MGP3Car

        Comment


        • #5
          Hi,
          well the low level stuff is possible in VB, but I don't fully understand what you are saying, also I want other programms to ignore the keystrokes, only my app may respond.

          now when I hit a key and winamp has the focus, winamp responds to the keyhit (obviously) and so does my app... my app should respond, but winamp shopuldn't
          If at first you don't succeed.........
          destroy all evidence you tried

          for info on t6369c LCD or Presslab's powersupply check:
          http://www.namms.tk <=updated!!

          Comment


          • #6
            Re: Probably no help at all

            Originally posted by MGP3Car
            marsjell,
            I don't know VB but in C++, a neat trick is to get a handle to the external programs window proc (FindWindow etc.), save the handle away, replace the window proc handle with your own proc handle (SetWindowLong) and in the new proc pass any messages that you don't want to handle back to the original proc (CallWndProg).
            I used this as I couldn't get get SetWindowFocus to work.
            I don't know if you can do this lower level stuff in VB.
            ~MGP3Car
            yes u can....he might need to define a structure
            ...let me look for the api call... i use that in C2/3/4
            Or you can go to this site http://www.allapi.net/ and download the API-Guide app there...tons of VB stuff with that app..
            abcd-1
            Author of CobraI,II,III and now CobraIV.
            You can contact me on AOL instant messenger....nick is cenwesi or cenwesi3

            Comment


            • #7
              thanks cenwesi,

              the api-guide is great, haven't solved my problem yet but.......
              If at first you don't succeed.........
              destroy all evidence you tried

              for info on t6369c LCD or Presslab's powersupply check:
              http://www.namms.tk <=updated!!

              Comment


              • #8
                marsjell,
                Basically the idea is that every program has a message handler (WndProc), this takes in events such as quit, getting focus, paint requests and keyboard input.
                If you can change which program (or sub proc?) gets called on these events then you can 'steal' the keyboard input.
                Replace the external programs WndProc with your own and then you will always effectively have focus.
                Of course it's not as easy as that because the external program will still want to handle messages that you are not interested in, so any message that you don't want to handle should be sent directly to the original, so you will need to store away the original reference before you overwrite it.

                winamp e.g.

                Normally:
                messages->winamp's WndProc
                With your mesage handler:
                messages->your WndProc->winamp's WndProc

                If you want the message then don't pass it on to winamp.

                If you would like the c++ code that I used for Winamp then I'll post it, or if cenwesi could post some Cobra VB code then that would obviously be better for you.

                Hope this explains it.

                ~MGP3Car

                Comment


                • #9
                  Hi, if you would be willing to post that part of your code please... I understand in theorie what you're saying, but have no idea how to do this in practise.

                  thanks!
                  If at first you don't succeed.........
                  destroy all evidence you tried

                  for info on t6369c LCD or Presslab's powersupply check:
                  http://www.namms.tk <=updated!!

                  Comment


                  • #10
                    Try this:

                    <CODE>
                    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

                    Private Function GetKey(ByVal vKey As KeyCodeConstants) As Boolean
                    GetKey = GetAsyncKeyState(vKey) And &H8000
                    End Function

                    Private Sub Timer1_Timer()
                    If GetKey(vbKey1) Then
                    Debug.Print "Down 1"
                    ElseIf GetKey(vbKey2) Then
                    Debug.Print "Down 10"
                    ElseIf GetKey(vbKey3) Then
                    Debug.Print "Down 100"
                    ElseIf GetKey(vbKey5) Then
                    Debug.Print "Play/Pause"
                    ElseIf GetKey(vbKey7) Then
                    Debug.Print "Up 1"
                    ElseIf GetKey(vbKey8) Then
                    Debug.Print "Up 10"
                    ElseIf GetKey(vbKey9) Then
                    Debug.Print "Up 100"
                    End If
                    End Sub

                    </CODE>

                    Add a timer control which has an interval of at least 100ms. This will do what I think you're asking.
                    Don't Click

                    Comment


                    • #11
                      hihi, think I adviced you to use this a while ago...

                      oops my mistake, tried to help andrew chappell with this solution , just looked it up :http://www.mp3car.com/vbulletin/show...5&pagenumber=3

                      but that IS what I'm using, problem is that when vblink or winamp has the focus, both my app AND WinAmp react to the keystrokes, I only want my app to react, WinAmp shouldn't
                      If at first you don't succeed.........
                      destroy all evidence you tried

                      for info on t6369c LCD or Presslab's powersupply check:
                      http://www.namms.tk <=updated!!

                      Comment


                      • #12
                        @marsjell

                        Here's the C++ code that I'm using:

                        I have two message procs in one Called WndProc which handles messages from my windows and one called WinampWndProc that I use to grab the winamp messages.

                        when the program starts (DLL init's) I repace winamps WndProc with my own:

                        In this case I have the hwnd (as it's a DLL plugin), I guess that if you want to do this then it would have to be a DLL.

                        //Get the original proc address
                        lpWndProcOld = (void *) GetWindowLong(plugin.hwndParent,GWL_WNDPROC);
                        //Set the address where winamp's win proc is
                        SetWindowLong(plugin.hwndParent,GWL_WNDPROC,(long) WinampWndProc);


                        so in WinampWndProc:

                        LRESULT CALLBACK WinampWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                        {
                        if(iwant the message)
                        do something with it
                        else
                        return CallWindowProc((long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long))lpWndProcOld,hwnd,message,wParam,lParam) ;
                        }

                        Anyway that's the C++ version, perhaps someone could post the VB equivalent?

                        ~MGP3Car

                        Comment


                        • #13
                          Hi,

                          so I'm guessing that "GWL_WNDPROC" determines which event to capture?
                          what value should this have?
                          If at first you don't succeed.........
                          destroy all evidence you tried

                          for info on t6369c LCD or Presslab's powersupply check:
                          http://www.namms.tk <=updated!!

                          Comment


                          • #14
                            Sort of (but not quite) GetWindowLong(plugin.hwndParent,GWL_WNDPROC); gets the address of the wndproc, this is the proc that handles all messages (~events) for the window refered to by hwndParent.

                            So by setting that value to your own wndproc, (using SetWindowLong) your proc will get the messages, not the original winamp one.

                            But if you get a message in your proc that you don't want, then you can still pass it back to the real winamp one using CallWindowProc with the lpWndProcOld that you got with GetWindowLong.

                            Comment


                            • #15
                              yes I understand, but I need to know which wndproc to alter therefor GWL_WNDPROC needs a value. (I think)

                              I do know that VB gives me an error if I do not assign a value to GWL_WNDPROC, or am I doing something wrong?
                              If at first you don't succeed.........
                              destroy all evidence you tried

                              for info on t6369c LCD or Presslab's powersupply check:
                              http://www.namms.tk <=updated!!

                              Comment

                              Working...
                              X