Block pasting any content inside the “textarea” or “code box area”

Hello everyone,

I am a professor at a Public University in Brazil and I am working on a project to help my students to program using python language. I found that CodeMirror is an excellent way to help them visualize their code work during this learning process because they can practice and meet desirable levels of performance while being guided for me. In the platform that I am designing, I am trying to “encourage” my students to type each letter/symbol or number used in the code, especially because some of them can be tempted to cheat their colleagues to get higher grades. Based on this concern, I would appreciate so much if someone could help me to block students to paste any content inside the “textarea” or “code box area”. I’ve already blocked the “copy”, “cut” functions inside that “textarea” by using the command “lineWiseCopyCut: false,” .

To sum up, the goal is that students should not be able to paste any content inside the “textarea” or “code box area” , rather, they will have to really work on developing the code by themselves by typing it in the “textarea”

Thank you so much!

Firstly, I feel this is probably a bad idea – crippling people’s interface isn’t helpful.

But if you really want to, you could use the beforeChange event to cancel changes whose origin is "paste".

Thank you so much for your answer, but this path is beyond my programming capacities. Do you mind in providing me another way to solve the problem? If there is such ease way.The reason why I am to block my student from pasting inside the textarea is because I want them to learn actually doing the homework rather than copying from someone else.

editor.on("beforeChange", function(_, change) {
  if (change.origin == "paste") change.cancel()

Where editor is your CodeMirror instance.

1 Like

Marijn, Thank you so much for your help! I really appreciate your help. It will be crucial in this project and It will benefit lots of Brazilians students. I will try to apply this line code that you sent. If I have any trouble, I hope it doesn’t bother you if I keep keep asking questions.

I am thrilled! It worked perfectly! Thank you so much!

worked perfectly., Thanks!