Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: overriding paste




Thank you Steve. Bug is fixed.

For future reference in case somebody else has to deal with this:

Set up this event handler for your control
{ kEventClassTextField, kEventTextShouldChangeInRange }

Call GetEventParameter( inEvent, kEventParamCandidateText.....

if the parameter is equal to one ( means it's a keystroke )
then call
SetEventParameter( inEvent, kEventParamReplacementText,

and return the appropiate result.

thanks,
SDM






On Wed, Apr 30, 2008 at 2:25 PM, Steve Christensen <email@hidden> wrote:
I dug the appended code out of an old project that I wrote a couple of years ago. Maybe compare the gist of what it's doing against your code?



On Apr 30, 2008, at 9:16 AM, Sam Mauney wrote:

Thanks Steve.

I've tried {kEventClassTextField, kEventTextShouldChangeInRange}.

It does help with blocking the "second" paste but then my keystrokes are filtered out
by the same event. To block the second paste I'm returning noErr and kEventParamReplacementText
paramater is not added to the event. All the other result scenarios allow keystrokes but also allow the double
paste.

Is there something else I can try?

thanks!
Sam


On Wed, Apr 30, 2008 at 11:56 AM, Steve Christensen <email@hidden> wrote:
On Apr 30, 2008, at 5:55 AM, Sam Mauney wrote:

I am currently calling a function where kHICommandPaste is tripped that uses Pasteboard
functions so I can filter the pasted contents into an edit control.

I am having trouble overrriding or suppressing the normal paste. So if I paste in the digit "2"
the result in the edit control will be "22".

What is the best way to override the normal paste so I can prevent this from happening?

Look into the Carbon events

 {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent}

for pre-10.4, or

 {kEventClassTextField, kEventTextShouldChangeInRange}

for 10.4 and later. If you install these on the edit control that you want to filter, you can control which characters are accepted by the control. It's a better solution than overriding paste in general.

steve


------------------------------------
OSStatus CFilteredEditTextView::EventHandler(CCarbonEvent& theEvent)
{
   OSStatus result = eventNotHandledErr;

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
   // handle filtering of text destined for the edit fields (10.0 - 10.3.9 method)
   if ((theEvent.GetClass() == kEventClassTextInput) &&
       (theEvent.GetKind() == kEventTextInputUnicodeForKeyEvent))
   {
       UInt32 dataSize = theEvent.GetParameterDataSize(kEventParamTextInputSendText, typeUnicodeText);

       if (dataSize > 0)
       {
           UniChar* data = "">
           if (data != NULL)
           {
               theEvent.GetParameter(kEventParamTextInputSendText, typeUnicodeText, dataSize, data);

               dataSize = sizeof(UniChar) * mFilter->EditTextFilter(*this, data, dataSize / sizeof(UniChar));

               theEvent.SetParameter(kEventParamTextInputSendText, typeUnicodeText, dataSize, data);

               free(data);

//              result = noErr;  // don't specifically set noErr so this will propogate automatically
           }
       }
   }
   else
#endif

   // handle filtering of text destined for the edit fields (10.4+ method)
   if ((theEvent.GetClass() == kEventClassTextField) &&
       (theEvent.GetKind() == kEventTextShouldChangeInRange))
   {
       CFStringRef candidateTextRef;

       theEvent.GetParameter(kEventParamCandidateText, candidateTextRef);
       if (candidateTextRef != NULL)
       {
           CCFString candidateText(candidateTextRef);
           CFIndex   length = candidateText.GetLength();

           if (length > 0)
           {
               result = userCanceledErr;

               UniChar* unicode = reinterpret_cast<UniChar*>(malloc(length * sizeof(UniChar)));

               if (unicode != NULL)
               {
                   candidateText.GetValue(unicode, length);

                   length = mFilter->EditTextFilter(*this, unicode, length);
                   if (length > 0)
                   {
                       CFStringRef replacementText = CFStringCreateWithCharacters(NULL, unicode, length);

                       if (replacementText != NULL)
                       {
                           theEvent.SetParameter(kEventParamReplacementText, replacementText);
                           result = noErr;
                       }
                   }

                   free(unicode);
               }
           }
       }
   }

   return result;
}


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

References: 
 >overriding paste (From: "Sam Mauney" <email@hidden>)
 >Re: overriding paste (From: Steve Christensen <email@hidden>)
 >Re: overriding paste (From: "Sam Mauney" <email@hidden>)
 >Re: overriding paste (From: Steve Christensen <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.