Permission denied while reading from clipboard

I wrote a small program to monitor the clipboard activity and print it to the terminal using reactive bananas and System.Clipboard.

module Main where

import Reactive.Banana
import Reactive.Banana.Frameworks
import System.Clipboard
import Data.Maybe(fromJust)

main :: IO ()
main = do
    sources <- (,) <$> newAddHandler <*> newAddHandler
    network <- setupNetwork sources
    actuate network
    s <- getClipboardString
    loop s sources

loop s (epop, epush) = do
    c <- getClipboardString
    if s /= c then
        snd epush [fromJust c]
    else
        loop s (epop, epush)

setupNetwork (epop, epush) = compile $ do
    ePop <- fromAddHandler $ fst epop
    ePush <- fromAddHandler $ fst epush

    bStack <- accumB [""] $ (++) <$> ePush
    eStack <- changes bStack

    reactimate' $ fmap print <$> eStack

      

When I run it, I go to another window (like chrome) and copy the text. When I do this, I get Main.hs: CloseClipboard: invalid argument (Thread does not have a clipboard open.)

. I am running this on windows. Any help would be great!

Greetings

+3


source to share


1 answer


After doing a little research, I believe I have found the cause of the problem. It so happened that when I copied the new text to the clipboard, the program locked the program so that no other changes would take place. During this blocking time, my program was trying to access the buffer and received an error message. To solve this problem, I turned the challenge getClipboardString

with the help of Either

using try

of Control.Exception

, and then compared the pattern of error.



c <- try getClipboardString :: IO (Either SomeException (Maybe String))
case c of
    Left err -> loop s (epop, epush) --ignore
    Right clip -> --do something usefull

      

0


source







All Articles