0

I want to determine that when I click on the Print Screen button on keyboard, is it written to the console whether it’s a image or not? Needs use buffer like

const checkHaveImageInClipboar = async () => {
        try {
            const clipboardItems = await window.navigator.clipboard.read();
            return clipboardItems[0].types.some(type => /image/.test(type));
        } catch (err) {
            return false;
        }
    };

It is on the button on the keyboard, and not on the button in the DOM . Because there may be a situation where you clicked on the screenshot on the keyboard, but the screen didn’t work, and I just want to check if there is a photo in the buffer, then output 'photo', otherwise output 'not photo' . Please help me

5
  • AFAIK PrintScreen works outside of the browser. Commented Oct 7, 2024 at 15:17
  • @evolutionxbox, i use window.addEventListener('keyup', event => { if (event.keyCode === 44 (it's keyCode for Screen) Commented Oct 7, 2024 at 15:20
  • 1
    What does not work with your approach? Commented Oct 7, 2024 at 15:29
  • 1
    Yes, you'll detect the keypress but that doesn't necessarily put something in the clipboard. Even if a screenshot is taken. On my computer, I get the screenshot in another piece of software (Greenshot) but not in the copy buffer. But conversely, if somebody already had an image in their clipboard and print screen "didn't work", then your code would get a false positive. Commented Oct 7, 2024 at 15:29
  • 1
    It's notable, that Clipboard stores data in multiple different formats, exact formats depend on the application copying data. Just for an example: Excel stores a copied selection as a png image as well as HTML among other formats. Commented Oct 7, 2024 at 15:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.