Craig Innes
Craig Innes

Reputation: 1573

Haskell SOEGraphics window wont close

I am currently following the exercises the in the book: "The Haskell School of Expression" and have reached the third chapter on creating graphics. The book uses the SOEGraphics module and demonstrates drawing some simple text in a window and then closing it with a button press.

However, when after compiling and executing, I find that although the window appears with the text on screen, the window refuses to close regardless of which keys I press or whether the focus is on the command line or the window itself.

Here is the source code from the book:

module Main where
import SOE
main =  runGraphics(
        do  w <- openWindow
                "My First Graphics Program" (300, 300)
            drawInWindow w (text(100,200) "HelloGraphicsWorld")
            k <- getKey w
            closeWindow w
        )

The only way to get the window to close is by forcing it to quit with CTRL-C. Is there something I have overlooked with my code? The program was compiled using GHC 7.4.1 and was run on Ubuntu.

Upvotes: 5

Views: 615

Answers (3)

J Adrian Zimmer
J Adrian Zimmer

Reputation: 7

Daniel is correct but I found it weird that getKeyEx works when getKey which merely uses getKeyEx does not. So I looked. The problem is pretty clear. Here is the existing code for getKey

getKey win = do
  ch <- getKeyEx win True
  if ch == '\x0' then return ch
    else getKeyEx win False

Here's what it should be

getKey win = do
  ch <- getKeyEx win True
  if ch /= '\x0' then return ch
    else getKeyEx win False

Make this fix and getKey works.

Upvotes: 0

Daniel
Daniel

Reputation: 118

I'm using the current release of the SOE package, which was released about 9 months prior to the date of the question. Like the poster of the question, I am running GHCi 7.4.1 on Ubuntu (12.04). I ran into this same issue, but DuckMaestro's answer did not apply:

getKeyChar is not defined in any of the modules in the SOE package. However, getKeyEx is:

getKeyEx :: Window -> Bool -> IO Char

Though undocumented in the SOE package itself, this function appears to emulate its namesake in the Graphics.HGL.Utils module.

However, whereas getKey is exported from the SOE module, getKeyEx is not.

In order to compile and have the window close on a keypress event

  1. exported getKeyEx from the SOE module
  2. in the code quoted in the question, changed k <- getKey w to k <- getKeyEx w True

Upvotes: 0

DuckMaestro
DuckMaestro

Reputation: 15875

Try getKeyChar intead of getKey. There seems to have been a change in the preferred method to use and/or to the behavior in certain OSs.

Upvotes: 2

Related Questions