It should be safe to use a keyboard and mouse that was previously used on a machine running malicious code.
This answer to a similar question on the Security SE site notes that some keyboards contain upgradeable firmware which could pose a threat:
...some keyboards have upgradable firmwares, so some malware on a machine could send a malicious upgrade to the keyboard, contaminating it permanently.
However the answer states (and I would still agree here in 2018) that the liklihood of this is low.
The same risk would apply to a mouse, though even fewer mice support firmware flashing than keyboards.
Anecdotally I've used the same five keyboards/mice to repair hundreds of computers over the years, including ones with nasty malware infections and I've never had any reason to suspect I was working with code that was capable of transmitting via those peripherals.
Not as much of a no-brainer as it would seem if you have high-end accessories. – fixer1234 – 2018-07-29T02:35:43.317