Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Ctrl+C

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

  • Ctrl+C

    I read from the winapi help file that ctrl+c, if I understand it
    correctly, is treated as a signal and not as keyboard input. I
    have a paste button on a toolbar that is disabled if the clipboard
    does not contain any text. What api call do I need to use to
    see if the ctrl+c signal has been generated so I then can enable the paste button
    as a result of it.

    Regards,
    Adam

    ------------------

  • #2
    The following is off the top of my head, so E&OE please...

    Normally, the control with keyboard focus will deal with Copy, Cut and Paste messages itself.

    It is usual design practice to enable/disable the copy/cut/paste toolbar buttons when processing Notification Messages from edit controls (and any other controls that can use Cut/Copy/Paste functions). For example, by capturing %WM_COMMAND|%EN_SETFOCUS and %WM_COMMAND|%EN_KILLFOCUS messages.

    For example, when an edit control gains keyboard focus, your callback will receive a %WM_COMMAND|%EN_SETFOCUS message. In response to this message, you would query the state of the text in the clipboard... if text was found to be present, then you would enable the Paste button (or disable it if there was no text on the clipboard).

    If you also examine the "current selection" state, and some text is found to be selected (ie, blue high-light), then you would enable the Cut/Copy buttons, otherwise disable them. This would usually be done in response to %EN_SETFOCUS notification and also in response to %EN_CHANGE, etc.

    Also, when focus is lost (%WM_COMMAND|%EN_KILLFOCUS), you would disable the toobar buttons, etc.

    In essence, you don't need to look at the keyboard activity to handle the operations... you only need to enable/disable the toolbar buttons according to the conditions of the current {edit} control.

    If you ignore the state of the actual clipboard and enabled the Paste button in response to a Cut/Copy "action", then your app will not accurately reflect the state of affairs if another application clears the clipboard, your app would need to readjust the toolbar button state when the control regains focus again. If you did not do it this way, then your button would remain enabled yet there would be no text to paste - this design would then not adhere to standard GUI "design concepts".

    One final note: I don't have my copy here to confirm, but IIRC Charles Petzolds "Programming Windows" book does discuss these concepts (or it could be in the Rector/Newcomer "Win32 Programming" book). When I get back to my office, I'll check it out (pls remind me if I forget! Thanks!)

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Yes, and you can use the IsClipboardFormatAvailable API to check
      if the clipboard contains anything to paste, for plain text like:

      CanPasteText = IsClipboardFormatAvailable(%CF_TEXT)



      ------------------


      [This message has been edited by Borje Hagsten (edited October 09, 2000).]

      Comment


      • #4
        I think that most of program use SetClipboardViewer.
        This short sample registrates (see caption) any change of Clipboard.

        Code:
           #Compile Exe
           #Dim All
           #Register None
           #Include "Win32Api.Inc"
        
           CallBack Function DlgProc
              Static hwndNextViewer As Long
              Select Case CbMsg
                 Case %WM_INITDIALOG
                    hwndNextViewer = SetClipboardViewer(CbHndl)
                 Case %WM_CHANGECBCHAIN
                    If CbWparam = hwndNextViewer Then hwndNextViewer = CbLparam Else _
                    If hwndNextViewer <> 0 Then SendMessage hwndNextViewer, CbMsg, CbWparam, CbLparam
                 Case %WM_DESTROY
                    ChangeClipboardChain CbHndl, hwndNextViewer
                 Case %WM_DRAWCLIPBOARD
                    Static n As Long: Incr n: SetWindowText CbHndl, Str$(n)
                    SendMessage hwndNextViewer, CbMsg, CbWparam, CbLparam
              End Select
          End Function
        
          Function PbMain
             Local hDlg As Long
             Dialog New 0, "Test", ,, 100, 100, %WS_CAPTION Or %WS_SYSMENU, %WS_EX_TOPMOST To hDlg
             Dialog Show Modal hDlg Call DlgProc
          End Function
        ------------------
        E-MAIL: [email protected]

        Comment


        • #5
          I am already using IsClipboardFormatAvailable api to check the
          clipboard so I can enable the Paste command in the edit menu as a
          repsonse to a %WM_INITMENU messaqge which does exactly what I
          want for it. I use a mousemove procedure to enable the cut,copy buttons
          on the toolbar as well. I am also already processing the %EN_SETFOCUS message
          to check the state of the clipboard to enable the paste button on the toolbar. I am sorry for not clarifying, I didn't mean to directly
          enable the paste button in response to the ctrl+c signal, I wanted
          to do a ctrl+c, check the clipbaord to see if there was text and
          then enable the paste button based on the contents of the clipboard.
          The edit control is the only control on the window, and oh by the way
          this is not a DDT app, and what if the window is maximaized. The edit
          control automatically gets the focus when the window gets the
          focus, so I am only getting one %EN_FOCUS message generated when
          the window gets the focus, correct? If there is no text on the
          clipboard to start with when the window gets the focus and I highlight
          some text and press ctrl+c, I want the paste button, like my paste command to become
          enabled. Like I said I have the paste menu item doing this by processing
          the %WM_INIMENU message. Now processing
          the %WM_KEYUP message(0r %WM_KEYDOWN), if I check to see if the "c" key
          or anyother key for that fact is pressed then I enable it just fine; of couse,
          no text is copied to the clipboard, because it is not ctrl+c,for me to check first. Since the
          ctrl+c is treated as a signal instead of keyboard input,according the help guide then I am out-of-luck at the moment.

          Regards,
          Adam

          ------------------

          Comment


          • #6
            Adam --
            I modified one old training code
            Code:
               #Compile Exe
               #Register None
               #Dim All
               #Include "win32Api.inc"
            
               Global ghKbrdHook As Long, hDlg As Long
               Function KeyboardHook(ByVal iCode As Integer, ByVal wParam As Long, ByVal lParam As Long) As Dword
                  Dim w As String, ww As Static String
                  If iCode = %HC_ACTION Then
                     If (lParam And &H80000000) = 0 Then ' Pressed
                        If Asc(Mkl$(lParam), 3) = 46 Then
                           If (GetKeyState(%VK_LCONTROL) And &H80000000) Or _
                              (GetKeyState(%VK_RCONTROL) And &H80000000) Then w$ = "Ctrl-C"
                        End If
                     End If
                     If w$ <> ww$ Then ww$ = w$: SetWindowText hDlg, ByCopy w$
                  End If
                  Function = CallNextHookEx(ghKbrdHook, iCode, wParam, lParam)
               End Function
            
               Function PbMain      
                  ghKbrdHook = SetWindowsHookEx(%WH_KEYBOARD, CodePtr(KeyboardHook), 0, GetCurrentThreadId)
                  Dialog New 0,"Test",,, 50, 50, %WS_CAPTION Or %WS_SYSMENU To hdlg
                  Dialog Show Modal hdlg
                  UnhookWindowsHookEx ghKbrdHook
               End Function
            but sure that there is a lot of other ways, how to detect Ctrl-C.


            ------------------
            E-MAIL: [email protected]

            Comment


            • #7
              Thanks for the code Semen but I could never get it to work - maybe
              it's a Windows ME issue, but I doubt it. If other windows apps
              can do this, then there is obviously a way to do it, hmmmm....

              Regards,
              Adam

              ------------------

              Comment


              • #8
                Why not simply use IsClipboardFormatAvailable in WM_KEYUP? Also,
                remember that Ctrl+Ins also works like Ctrl+C, Shift+Ins like
                Ctrl+V and Shift+Delete like Ctrl+X..

                ------------------

                Comment


                • #9
                  Hi Borje,
                  Thanks for the suggestion but I tried that and it did not work
                  either. The hlp guide says this about ctrl+c and ctrl+break:

                  The CTRL+C and CTRL+BREAK key combinations receive special
                  handling by console processes. By default, when a console
                  window has the keyboard focus, CTRL+C or CTRL+BREAK is treated
                  as a signal (SIGINT or SIGBREAK) and not as keyboard input.
                  By default, these signals are passed to all console processes
                  that are attached to the console, causing the system to call
                  the control handler function or functions associated with these
                  processes. Detached processes (GUI processes or console processes
                  started with the DETACHED_PROCESS or CREATE_NEW_CONSOLE flag)
                  are not affected.

                  So Other keystrokes work but not ctrl+c. I will keep playing with it
                  though and see if something "pops up".

                  Regards,
                  Adam

                  ------------------

                  Comment


                  • #10
                    Adam --

                    Ah, you didn't mention until now that you were talking about a console window!

                    PB/CC automatically handles the function that allows Ctrl-C to be passed to your program instead of acting like a Ctrl-Break "signal".

                    If you are using PB/DLL and creating a console window with AllocConsole, check out the SetConsoleMode(ENABLE_PROCESSED_INPUT) API.

                    -- Eric

                    ------------------
                    Perfect Sync: Perfect Sync Development Tools
                    Email: mailto:[email protected][email protected]</A>



                    [This message has been edited by Eric Pearson (edited October 09, 2000).]
                    "Not my circus, not my monkeys."

                    Comment


                    • #11
                      Adam -
                      Initial code was tested under 2000.
                      I forgot about differences with 9x.
                      In this form it works under 98SE.
                      Under 2000 - it's necessary to change VK_CONTROL to VK_LCONTROL.
                      Code:
                         #Compile Exe
                         #Register None
                         #Dim All
                         #Include "win32Api.inc"
                         Global ghKbrdHook As Long, hDlg As Long
                         Function KeyboardHook(ByVal iCode As Integer, ByVal wParam As Long, ByVal lParam As Long) As Dword
                            Dim w As String, ww As Static String
                            If iCode = %HC_ACTION Then
                               If (lParam And &H80000000) = 0 Then ' Pressed
                                  If Asc(Mkl$(lParam), 3) = 46 Then
                                     If (GetAsyncKeyState(%VK_CONTROL) And &H80000000) Or _
                                        (GetAsyncKeyState(%VK_RCONTROL) And &H80000000) Then w$ = "Ctrl-C"
                                  End If
                               End If
                               If w$ <> ww$ Then ww$ = w$: SetWindowText hDlg, ByCopy w$
                            End If
                            Function = CallNextHookEx(ghKbrdHook, iCode, wParam, lParam)
                         End Function
                         Function PbMain
                            ghKbrdHook = SetWindowsHookEx(%WH_KEYBOARD, CodePtr(KeyboardHook), 0, GetCurrentThreadId)
                            Dialog New 0,"Test",,, 50, 50, %WS_CAPTION Or %WS_SYSMENU To hdlg
                            Dialog Show Modal hdlg
                            UnhookWindowsHookEx ghKbrdHook
                         End Function
                      ------------------
                      E-MAIL: [email protected]

                      Comment


                      • #12
                        Thanks for the suggeston Eric, but right after I posted my last
                        message I fixed the problem and it is working now. It turned out
                        to be a logic problem(Hello, earth to Adam - come in Adam). I also
                        misread the help guide about mentioning about a CONSOLE window -
                        DUHHHHHHHH. Sometimes it takes awhile for certain words to sink
                        in I guess. I am not working with a console window. Well at least
                        it is working now. Thank you gentlemen for offering to help though.

                        Regards,
                        Adam


                        ------------------

                        Comment

                        Working...
                        X