0

i have a custom context menu for canvas, but with Text elements i have a problem, when i try to click on text element by right-click i have text selection (like double click).

When i use try to use:

document.addEventListener('contextmenu', (e) => {
   e.preventDefault();
})

its couldn't help, because i have text selection.

enter image description here

AlexWink
  • 11
  • 6

2 Answers2

0

It is not really "answer-worthy", but due to my low reputation, I cannot write a comment. So, that being said, you have events like "onCopy", "onCut", "onPaste", etc. You can disable each and every one of them individually. You can see an example of that here:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <h2>Copy, cut and paste disabled</h2>
    <input type="text" onselectstart="return false" onpaste="return false;" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off/>
    <br>
  </body>
</html>

If you have any further questions - here is the full article.

Stan
  • 72
  • 5
  • thx, i saw this article, but i need when i have right-click event on selected element, maybe i can't explain with my bad english, for example on site canva.com, u add text element on workspace, and try to click right click and word in text not selecting – AlexWink Jan 14 '22 at 16:05
  • You have seen this **[article](https://stackoverflow.com/questions/12805803/disable-copy-paste-in-html-input-fields)**. I think that might do. However, this is something very easy to bypass and you cannot really restrict that kind of behavior persistently imo. – Stan Jan 17 '22 at 07:56
0

Sometimes inline javascript expression is a good trick.Forexample form tags we are writing

<form id="test" onsubmit="return false;">

So I based on this example. maybe you can try this

<canvas oncontextmenu="return false;"></canvas>

edit after comments : which button actication you need excalty I dont know, but maybe you can try to find 1,2,3,4...

  document.addEventListener('contextmenu', function(e) {
   // forexample right-click is equal to two
    if (e.button == 2) {
      // Block right-click menu, thru preventing default action
      e.preventDefault();
    // if its not work with e.preventDefault(); add next line return false;
    }
  });
huzeyfetas
  • 86
  • 2
  • 6
  • sry, but its not in my case – AlexWink Jan 14 '22 at 16:22
  • okey ,then. can you also try this `document.addEventListener('contextmenu', (e) => { e.preventDefault(); return false; })` – huzeyfetas Jan 14 '22 at 16:29
  • i try to explain a problem, when i do u example, first click by right click its good, but second click i have selecting text, but i dont need it, i need selecting text just by double click – AlexWink Jan 14 '22 at 16:48
  • I edited my solution, can you try it please ? – huzeyfetas Jan 14 '22 at 17:18
  • its dosen't work, maybe its help for example, go to https://www.canva.com/design/play?category=tACFal755_E&locale=en-GB&analyticsCorrelationId=61bbd3d0-9b61-47ea-8ae9-a7c5e68c00db add text from the left tab – AlexWink Jan 14 '22 at 17:47
  • maybe it would be a solution for you , https://stackoverflow.com/a/43321596/11842307 – huzeyfetas Jan 14 '22 at 18:12
  • e.button == 2 its for double click ( – AlexWink Jan 15 '22 at 09:52
  • visit last link which I shared. you will find a lot of solution in there. maybe u need just css code like `user-select: none` ı dont know. again visit last link. maybe u need mouse.eventDetail>1 Idk, or e.button == 2 for duble click or u try e.button == 0 for one click. or maybe u need to listen mouseDown event .. share ur last state with me pls if u find a solution pls edit the post. – huzeyfetas Jan 15 '22 at 11:53