Let's go back to basics.
How long has the issue been occurring?
Did the mouse and keyboard issue start occurring at the same time?
What model iMac do you have?
What version of macOS are you running?
What brand/model mouse and keyboard are you using? (Your previous screenshots indicate wireless, but no details)
If you are using the current generation Apple Wireless Keyboard, are you using it in the wireless mode, or with the USB->Lightning cable plugged in? If it's in wireless mode, try connecting the USB->Lightning cable so it works in the wired mode to see if the issue changes.
Has there ever been another mouse and/or keyboard paired to this iMac? A couple years ago I purchased a new keyboard with a keypad and set the old wireless in a pile of stuff on my desk. A couple weeks later my iMac started acting strange as if someone was pressing keys. After much frustration I found my old keyboard buried under a pile of stuff that had been piled on top of my desk. I had forgotten to turn the old keyboard off and the weight of the pile was randomly pressing keys as things were shuffled about. I learned my lesson. You can check your Bluetooth preferences to see what devices are paired with your iMac, then delete any older devices that you are no longer using with your system. Be careful not to delete your current keyboard and mouse.
If you have another keyboard and mouse, try disconnecting and powering down (if applicable) the old keyboard/mouse to see if the issue goes away. You indicated that the cursor moves and the keys on the keyboard are also apparently being pressed to bring up the diacritical menu which is odd and could be an indication that software could be involved. Have you installed any new software or utilities recently?
It may also be helpful to create another user account and see if the issue also occurs there. If the issue disappears then look closely at your problematic user account and troubleshoot for software/configuration issues. You can also use Safe Mode to help determine whether you are experiencing a software issue. Here's more information:
Use safe mode to isolate issues with your Mac - Apple Support
Although I can't imagine what parameter could generate the type of issue you described, resetting the NVRAM won't hurt, and could possibly help. For more information refer to the following Apple article:
Reset NVRAM or PRAM on your Mac - Apple Support
It would be helpful to understand what you mean by the cursor moving on its own. How far does the cursor move? Does it appear to move in a controlled fashion, or does it jump around? How long does it last? What, if anything, do you do to stop it so you regain control of the iMac? Is restarting the only solution? Do you have any kids, friends, (or cats?) that might be playing a practical joke on you?
Sorry to throw so many questions and information at you. I'm just trying to figure out all of the different ways that this type of issue could occur.