def setup_ph_dict():
    #Create a Dictionary object and a key listener object
    try:
        dictionary = Dictionary()

    except RuntimeError as e:
        print("Runtime Exception: %s" % e.details)
        print("Exiting....")
        exit(1)

    try:
        dictionary.setErrorHandler(DictionaryError)
        dictionary.setServerConnectHandler(DictionaryServerConnected)
        dictionary.setServerDisconnectHandler(DictionaryServerDisconnected)

    except PhidgetException as e:
        print("Phidget Exception %i: %s" % (e.code, e.details))
        print("Exiting....")
        exit(1)

    print("Opening phidget object....")

    try:
        dictionary.openRemoteIP('localhost', port=5001)
    except PhidgetException as e:
        print("Phidget Exception %i: %s" % (e.code, e.details))
        print("Exiting....")
        exit(1)

    try:
        while dictionary.isAttachedToServer() == False:
            pass
        else:
            print("Connected: %s" % (dictionary.isAttachedToServer()))
            print("Server: %s:%s" %
                  (dictionary.getServerAddress(), dictionary.getServerPort()))
    except PhidgetException as e:
        print("Phidget Exception %i: %s" % (e.code, e.details))
        print("Exiting....")
        exit(1)
    return dictionary
#Basic imports
import random
import sys
from ctypes import *
from time import sleep

#Phidget specific imports
from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException
from Phidgets.Events.Events import ErrorEventArgs, KeyChangeEventArgs, \
                                ServerConnectArgs, ServerDisconnectArgs
from Phidgets.Dictionary import Dictionary, DictionaryKeyChangeReason, \
                                KeyListener

#Create a Dictionary object and a key listener object
try:
    dictionary = Dictionary()
    keyListener = KeyListener(dictionary, ".*")
except RuntimeError as e:
    print("Runtime Exception: %s" % e.details)
    print("Exiting....")
    exit(1)


#Event Handler Callback Functions
def DictionaryError(e):
    print("Dictionary Error %i: %s" % (e.eCode, e.description))
    return 0


def DictionaryServerConnected(e):
    print("Dictionary connected to server %s" % (e.device.getServerAddress()))