wxPython TextCtrl overwrite default paste method

Hi everyone

I have a TextCtrl and i want to add a newline at the end of the pasted text and
i was wondering if it is possible to overwrite the default paste behaviour to achieve this.

I don’t want to bind the same handler on multiple events i just want to overwrite the
default paste behaviour if it is possible. For example on linux you can paste text
using the CTRL + v but you can also use the mouse middle click which does not trigger
the EVT_TEXT_PASTE. In a normal situation i would had to bind the same handler both on
EVT_TEXT_PASTE and on EVT_MIDDLE_DOWN it’s not hard to do but i am just wondering
if i could overwrite the default paste behaviour this way if a new “shortcut” was about to be
added i wouldnt had to make any changes and bind new events.

(Also i couldnt find any solutions on google)

Here is the issue on github: https://github.com/MrS0m30n3/youtube-dl-gui/issues/29
And the latest commit for the EVT_TEXT_PASTE handler: https://github.com/MrS0m30n3/youtube-dl-gui/commit/9dce6299e93c781d76e68b91cd216a4dee30bf21

Sorry if i made any mistakes, English is not my native language.

ytubedlg@gmail.com wrote:

Hi everyone

I have a TextCtrl and i want to add a newline at the end of the pasted
text and
i was wondering if it is possible to overwrite the default paste
behaviour to achieve this.

I don't want to bind the same handler on multiple events i just want to
overwrite the
default paste behaviour if it is possible. For example on linux you can
paste text
using the CTRL + v but you can also use the mouse middle click which
does not trigger
the EVT_TEXT_PASTE. In a normal situation i would had to bind the same
handler both on
EVT_TEXT_PASTE and on EVT_MIDDLE_DOWN it's not hard to do but i am just
wondering
if i could overwrite the default paste behaviour this way if a new
"shortcut" was about to be
added i wouldnt had to make any changes and bind new events.

I'm not quite sure what exactly you are looking for but if you want to change what will be pasted then the normal way to do something like that would be to fetch the data from wx.TheClipboard yourself, modify it and then put it in the textctrl yourself. You could do this from the event handler for the keyboard shortcuts, or anywhere else.

···

--
Robin Dunn
Software Craftsman

Basically i was wondering if i could do domething like this:

textctrl = wx.TextCtrl(panel, style = wx.TE_MULTILINE)

And then overwrite the Paste method like so:
textctrl.Paste = MyPaste

OR

wx.core.TextEntryBase_Paste = MyPaste

Then just by hitting CTRL + v i could basically add a newline because instead of the default Paste method i could use mine.
I am confused with how the Paste occurs. Does the default Paste() method gets called when we hit CTRL + v or does the event
gets handled directly at the wxWidgets (C/C++) level?

···

On Tuesday, March 17, 2015 at 1:04:22 AM UTC+2, ytub...@gmail.com wrote:

Hi everyone

I have a TextCtrl and i want to add a newline at the end of the pasted text and
i was wondering if it is possible to overwrite the default paste behaviour to achieve this.

I don’t want to bind the same handler on multiple events i just want to overwrite the
default paste behaviour if it is possible. For example on linux you can paste text
using the CTRL + v but you can also use the mouse middle click which does not trigger
the EVT_TEXT_PASTE. In a normal situation i would had to bind the same handler both on
EVT_TEXT_PASTE and on EVT_MIDDLE_DOWN it’s not hard to do but i am just wondering
if i could overwrite the default paste behaviour this way if a new “shortcut” was about to be
added i wouldnt had to make any changes and bind new events.

(Also i couldnt find any solutions on google)

Here is the issue on github: https://github.com/MrS0m30n3/youtube-dl-gui/issues/29
And the latest commit for the EVT_TEXT_PASTE handler: https://github.com/MrS0m30n3/youtube-dl-gui/commit/9dce6299e93c781d76e68b91cd216a4dee30bf21

Sorry if i made any mistakes, English is not my native language.

...

Then just by hitting CTRL + v i could basically add a newline because instead of the default Paste method i could use mine.
I am confused with how the Paste occurs. Does the default Paste() method gets called when we hit CTRL + v or does the event
gets handled directly at the wxWidgets (C/C++) level?

Have a look at wx.lib.masked it overrides Paste.

Werner

···

On 3/18/2015 11:29, ytubedlg@gmail.com wrote:

Have a look at wx.lib.masked it overrides Paste.

Hi, werner

Can you become more specific ?
From the documentation of the wx.lib.masked wxPython API Documentation — wxPython Phoenix 4.2.2 documentation
“allowing characters within a data entry control to remain fixed, and providing fine-grain control over allowed user input.”
I can’t understand how this is going to help me change the default paste behaviour.
Can you give me an example?

I was thinking of the code not the doc, in particular.
wx.lib.masked.textctrl in there look at BaseMaskedTextCtrl.Paste and
at wx.lib.masked.maskededit where the masked specific paste code is
in MaskedEditMixin._Paste.
Werner

···

Hi,

  On 3/18/2015 14:49, wrote:

ytubedlg@gmail.com

        Have a look at wx.lib.masked it overrides

Paste.

Hi, werner

      Can you become more specific ?

      From the documentation of the wx.lib.masked

“allowing characters within a data entry control to remain
fixed, and providing fine-grain control over allowed user
input.”
I can’t understand how this is going to help me change the
default paste behaviour.
Can you give me an example?

http://www.wxpython.org/docs/api/wx.lib.masked-module.html

I was thinking of the code not the doc, in particular.

wx.lib.masked.textctrl in there look at BaseMaskedTextCtrl.Paste and

at wx.lib.masked.maskededit where the masked specific paste code is
in MaskedEditMixin._Paste.

Werner

Hi, Werner

I found the time and i looked at the code. Correct me if am wrong but in the above code there is an event handler bind on MaskedEditMixin._OnCtrl_V which basically calls the Paste() method.

The behaviour is like this:

class MyTextCtrl(wx.TextCtrl):

def __init__(self, *args, **kwargs):                                       
    wx.TextCtrl.__init__(self, *args, **kwargs)                            
                                                                           
    self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)                             
                                                                           
def OnKeyDown(self, event=None):                                           
    if event.CmdDown() and event.GetKeyCode() == 86:                       
        self.Paste()                                                       
    else:                                                                  
        event.Skip()                                                       
                                                                           
def Paste(self):                                               
    print 'Custom paste'                                                   
    wx.TextCtrl.Paste(self)

and i am asking if it is possible to do something like this:

class MyTextCtrl(wx.TextCtrl):

def __init__(self, *args, **kwargs):                                       
    wx.TextCtrl.__init__(self, *args, **kwargs)                                                                         
                                                                           
def Paste(self):                                               
    print 'Custom paste'                                                   
    wx.TextCtrl.Paste(self)

Hhm, don’t know way Will Sadkin (author of masked) did use the
OnKeyDown handler also for Paste.
I would just give your method a try.
Note that Paste is defined in TextEntry which TextCtrl is
inheriting, maybe that is way Will did it with the OnKeyDown
handler.
Werner

···

Hi,

  On 3/28/2015 12:21, wrote:

ytubedlg@gmail.com

        I was thinking of the code not the doc, in

particular.

        wx.lib.masked.textctrl in there look at

BaseMaskedTextCtrl.Paste and at wx.lib.masked.maskededit
where the masked specific paste code is in
MaskedEditMixin._Paste.

        Werner

Hi, Werner

      I found the time and i looked at the code. Correct me if am

wrong but in the above code there is an event handler bind on
MaskedEditMixin._OnCtrl_V which basically calls the Paste()
method.

      The behaviour is like this:



      class

MyTextCtrl(wx.TextCtrl):

          def __init__(self, *args,

**kwargs):

              wx.TextCtrl.__init__(self, *args,

**kwargs)

              self.Bind(wx.EVT_KEY_DOWN,

self.OnKeyDown)

          def OnKeyDown(self,

event=None):

              if event.CmdDown() and event.GetKeyCode() ==

86:

self.Paste()

else:

event.Skip()

          def

Paste(self):

              print 'Custom

paste’

              wx.TextCtrl.Paste(self)
      and i am asking if it is possible to do something like

this: