User avatar
b1qb0ss
Topic Author
Posts: 19
Joined: 09 Feb 2015, 15:10

[Unity] Disable Textbox text Copy / Paste / Cut

17 Dec 2015, 02:44

Hello,
First I'm new to Noesis

since Unity isn't using .Net 4 i can't use the disable the text selection from textboxes
i tried detecting the Control Key and set the selection length to 0 but that doesn't work i still can select text

Thanks in Advance
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: [Unity] Disable Textbox text Copy / Paste / Cut

19 Dec 2015, 12:37

HI,

To disable Copy/Cut/Paste from a TextBox you can detect when their KeyBindings are pressed to avoid executing the corresponding commands:
public class DisableCopyCutPaste: MonoBehaviour
{
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
        var content = gui.GetContent();

        var txt = (TextBox)content.FindName("myTextBox");
        txt.PreviewKeyDown += txt_PreviewKeyDown;
    }

    void txt_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        Keyboard kb = ((UIElement)sender).GetKeyboard();
        bool ctrl = kb.IsKeyDown(Key.Control);
        bool shift = kb.IsKeyDown(Key.Shift);

        if ((ctrl && e.Key == Key.C) || (ctrl && e.Key == Key.Insert) ||
            (ctrl && e.Key == Key.X) || (shift && e.Key == Key.Delete) ||
            (ctrl && e.Key == Key.V) || (shift && e.Key == Key.Insert))
        {
            e.Handled = true;
        }
    }
}
 
User avatar
b1qb0ss
Topic Author
Posts: 19
Joined: 09 Feb 2015, 15:10

Re: [Unity] Disable Textbox text Copy / Paste / Cut

19 Dec 2015, 20:52

HI,

To disable Copy/Cut/Paste from a TextBox you can detect when their KeyBindings are pressed to avoid executing the corresponding commands:
public class DisableCopyCutPaste: MonoBehaviour
{
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
        var content = gui.GetContent();

        var txt = (TextBox)content.FindName("myTextBox");
        txt.PreviewKeyDown += txt_PreviewKeyDown;
    }

    void txt_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        Keyboard kb = ((UIElement)sender).GetKeyboard();
        bool ctrl = kb.IsKeyDown(Key.Control);
        bool shift = kb.IsKeyDown(Key.Shift);

        if ((ctrl && e.Key == Key.C) || (ctrl && e.Key == Key.Insert) ||
            (ctrl && e.Key == Key.X) || (shift && e.Key == Key.Delete) ||
            (ctrl && e.Key == Key.V) || (shift && e.Key == Key.Insert))
        {
            e.Handled = true;
        }
    }
} 
thank you, i appreciate that i didn't think about using PreviewKeyDown

Regards

Who is online

Users browsing this forum: No registered users and 84 guests